After continuous error messages I gave up on using UNION to join SELECT statements...then I ran across this post

http://drupal.org/comment/reply/557318/1963596

and assumed from the discussion that the UNION operator could not be used with drupal database queries? So I attempted several variations as the post suggested -- the lastest being:

// get the terms
			
$t_result = db_query("SELECT name FROM {term_data}
		WHERE tid IN ($terms)", array('$terms' => array(362, 366, 355)));
					
		$terms = array();
		while($t_record = db_fetch_object($t_result)){
			array_push($terms,$t_record->name);
		};
			$terms = implode(', ',$terms);
		}else{
			$terms = 'any';
		};

which returns errors such as:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2 query: SELECT name FROM term_data WHERE tid IN () in

Q) Can someone explain my syntax error?
Q) Is it possible to use one of these methods with drupal queries -- or should I just make multiple calls to the database to get my desired result?

PS -- I have simplified the above query for this example. Thanks for any assistance...I am running drupal 6.14

Comments

auctionteamster’s picture

I went back and tried to use the UNION operator and with a little hacking was able to get working code:

$x_terms = x;
$y_terms = y;

$SQL = "SELECT name FROM {term_data}
					WHERE tid IN ($x_terms)
					UNION
					SELECT name FROM {term_data}
					WHERE tid IN ($y_terms)";

NOTE: Don't do it this way...as per MySql handbook...http://dev.mysql.com/doc/refman/5.0/en/union.html

$SQL = "SELECT name FROM {term_data}
					WHERE tid IN ($x_terms)"
					UNION
					"SELECT name FROM {term_data}
					WHERE tid IN ($y_terms)";