Jump to content

MySQL and PHP Woes


adamwhiles

Recommended Posts

I'm having an issue selecting rows across multiple tables. Basically I have tables monday, tuesday, wednesday, thursday and friday and they all have a common field named lode. The field lode is not the same for each row across the tables like a primary key, lode is different in most instances. What I need to do is search all tables and find the rows where lode is equal to 7.

 

I tried this but figured it couldn't be that easy and it didn't work. Any ideas?

 

$result = mysql_query("SELECT * FROM monday, tuesday, wednesday, thursday, friday WHERE lode='7'") or die(mysql_error());

Link to comment
Share on other sites

Thanks! It works somewhat now. Now it creates a lot of duplicate entries. Here is the whole code segment.

 

$result = mysql_query("SELECT * FROM monday m, tuesday t, wednesday w, thursday th, friday f WHERE m.lode='7' OR t.lode='7' OR w.lode='7' OR th.lode='7' OR f.lode='7'") or die(mysql_error());
?>
<form action="test.php" name="unavailable_users" method="post">
<select name="listbox" size="3">
<?
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="Option 1"><?echo "$row[name]"; ?>
<?
}
?>
?>
</select>
<input type="submit" name="edit_user_go" value="Make User Available" />
</form>


<?

Link to comment
Share on other sites

basically I have 5 tables that look like this for monday through friday

CREATE TABLE IF NOT EXISTS `monday` (
  `name` varchar(60) NOT NULL,
  `lode` int(1) NOT NULL default '0',
  `slot1` int(1) NOT NULL default '0',
  `slot2` int(1) NOT NULL default '0',
  `slot3` int(1) NOT NULL default '0',
  `slot4` int(1) NOT NULL default '0',
  `slot5` int(1) NOT NULL default '0',
  `slot6` int(1) NOT NULL default '0',
  `slot7` int(1) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

name represent the intructors name, load represents the number of slots the instructor is booked for on that day and each slot represents a time slot for that day

Link to comment
Share on other sites

Since the 7 tables are not JOINed in anyway, you are definitely getting a cartesian product.

I think you are going to have to use a UNION:

 

SELECT 'Mon' as Day, m.* FROM monday m WHERE lode='7' 
UNION
SELECT 'Tue' as Day,t.* FROM tuesday t WHERE lode = '7'
UNION  
...

 

OR - I would consider combining the tables into one and adding a column for the Day of Week:

 

CREATE TABLE IF NOT EXISTS `weekdays` (
  `dayofweek` varchar(9) NOT NULL,
  `name` varchar(60) NOT NULL,
  `lode` int(1) NOT NULL default '0',
  `slot1` int(1) NOT NULL default '0',
  `slot2` int(1) NOT NULL default '0',
  `slot3` int(1) NOT NULL default '0',
  `slot4` int(1) NOT NULL default '0',
  `slot5` int(1) NOT NULL default '0',
  `slot6` int(1) NOT NULL default '0',
  `slot7` int(1) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.