Page 1 of 1

UNION Problem!

Posted: Mon Jun 07, 2004 1:07 pm
by Joe
I am trying to select data from another table using UNION ALL SELECT but I seem to be getting continuous errors saying, You have an error in your SQL syntax near 'UNION ALL SELECT products FROM shopping_cart' at line 1. This is my code:

Code: Select all

<?php
$sql = "UNION ALL SELECT products FROM shopping_cart";
$res = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($res))
{
  $row = mysql_fetch_assoc($res);
  echo $row['products'];
}
Does anyone see any errors in my code that could be repaired. Thanks!


Joe 8)

Posted: Mon Jun 07, 2004 1:32 pm
by feyd
mysql manual wrote:(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10)
ORDER BY a;

Posted: Mon Jun 07, 2004 5:36 pm
by launchcode
UNION is only available as of MySQL 4.x and above - which is always worth checking (my host still uses 3.x for example) - also in your given example I can't even see the need for a union anyway as you only seem to be bringing back one table - but that might just be the way you wrote it. If all you want to do is bring back everything from 2 un-related tables (that cannot be joined) it's just:

Code: Select all

SELECT a, b, c FROM table_one
UNION
SELECT a, b, c FROM table_two;