Click to See Complete Forum and Search --> : 2 tables problem
AvisSoft
Sep 16th, 2004, 05:39 AM
Hi!
I have 2 tables... named acc_categroies and wnd_categories both have 1 field named categories...
i need to get all data from both tables then sort them and display how can i do this in 1 go ?
currenntly i use 2 sql queries to get the data from 2 tables..! and order it like this:
"select category from acc_categroies order by category" then another query
"select category from wnd_categories order by category"
now the problem is the data is dsiplayed liek:
A-Z for first tables then A-Z for second table.
Instead it should be A_Z for both tables!
AvisSoft
Sep 16th, 2004, 05:46 AM
Currently i am using this code:
<select name="position" id="position">
<?php
$connection = mysql_connect ("xxxx.xxx", "xxx_table", "xxxx") or die ("Unable to connect!");
$db = mysql_select_db ("xxxx", $connection) or die ("Unable to select database");
$sql = "select category from acc_categories ORDER BY category";
$sql_result = mysql_query($sql, $connection) or die ("Unable to execute query");
while ($row = mysql_fetch_array($sql_result))
{
$cat = $row["category"];
echo "<option>" . $cat . "</option>\n";
}
$sql = "select category from wnd_categories ORDER BY category";
$sql_result = mysql_query($sql, $connection) or die ("Unable to execute query");
while ($row = mysql_fetch_array($sql_result))
{
$cat = $row["category"];
echo "<option>" . $cat . "</option>\n";
}
mysql_free_result($sql_result);
mysql_close($connection);
?>
</select>
ober0330
Sep 16th, 2004, 07:40 AM
Well... I'm sure there is a way to combine it into one SQL statement... you'd have to use a join, but I'm not sure on the sorting part. That would be your fastest and easiest method, but my brain isn't working that fast this morning.
Your other option would be to still use 1 query with a join and dump everything to an array and then sort the array. Then you can dump that all out in your options.
CornedBee
Sep 16th, 2004, 08:34 AM
Starting with MySQL 4.0.0 you can use UNION:
(SELECT category FROM acc_categories)
UNION ALL
(SELECT category FROM wnd_categories)
ORDER BY category;
AvisSoft
Sep 18th, 2004, 07:05 AM
Originally posted by CornedBee
Starting with MySQL 4.0.0 you can use UNION:
(SELECT category FROM acc_categories)
UNION ALL
(SELECT category FROM wnd_categories)
ORDER BY category;
Thanks corned bee it helped! ;-)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.