Hi Ronnie,
r937 has some straight forward points there and you should try re-designing your database to amalgamate the tables (and then use normalisation) in order to ease the strain on your queries later. Also, you can easily work with seperate queries to solve this problem and combine the results in PHP.
In the meantime though, keeping it all in one query... (By the way, if you are a real sucker for punishment then look at this sick individuals incredible query).
The UNION syntax is implemented in MySQL 4 onwards, so if your server is still 3.xx then consider upgrading to use this. If you are stuck on the versions of MySQL which will not handle UNIONs then there are a couple of options open to you:
- Use the OR conditional as I mentioned before (but use the revised code below)
- Create a temporary table on the MySQL server to hold the query results from several queries and then return them (effectively a UNION but without having to rely on the function being available)
Option 1
PHP Code:
<?
$sql = "SELECT * FROM " .
"table1.catagory AS table1_cat, " .
"table2.catagory AS table2_cat, " .
"table3.catagory AS table3_cat, " .
"table4.catagory AS table4_cat, " .
"WHERE " .
"table1.catagory = '$catagoryname' OR " .
"table2.catagory = '$catagoryname' OR " .
"table3.catagory = '$catagoryname' OR " .
"table4.catagory = '$catagoryname'"
?>
Option 2
Code:
CREATE TEMPORARY TABLE tmp
SELECT * FROM table1 WHERE catagory = '$catagoryname';
INSERT INTO tmp
SELECT * FROM table2 WHERE catagory = '$catagoryname';
INSERT INTO tmp
SELECT * FROM table3 WHERE catagory = '$catagoryname';
INSERT INTO tmp
SELECT * FROM table4 WHERE catagory = '$catagoryname';
SELECT * from tmp;
DROP TABLE tmp;
Bookmarks