Reputation: 981
Is there a better way than this?
SELECT * FROM tagcloud
WHERE (usertag_tag = 1
OR usertag_tag = 2
OR usertag_tag = 3
OR usertag_tag = 4)
What if I want to add more tags to the query, do I keep adding OR statements?
Upvotes: 2
Views: 171
Reputation: 23978
Use MySQL IN
SELECT * FROM tagcloud
WHERE (usertag_tag = 1
OR usertag_tag = 2
OR usertag_tag = 3
OR usertag_tag = 4)
/* You are checking whether usertag_tag is either 1, 2, 3 OR 4*/
Is equivalent to:
SELECT * FROM tagcloud
WHERE (usertag_tag IN (1, 2, 3, 4))
/* You are checking usertag_tag is one of the values in the array given as
array(1, 2, 3, 4)
If you want to add more values, just add elements to the array, that is it.
*/
Explanation:
If we are comparing single value, we use =
.
If we need to find a rows with given field in one of the values (array of values), we use IN
.
MySQL IN
functions logically same as PHP's in_array()
Upvotes: 0
Reputation: 7834
you can use a simple version
SELECT * FROM tagcloud
WHERE usertag_tag in (1,2,3,4);
Hope this helps
Upvotes: 3
Reputation: 413
You could use an array like this,
$sql = "SELECT * FROM tagcloud WHERE user_tag IN ";
$j = 6;
for($i = 0; $i< $j; $i++){
$queryArray[] .= "('". $i. "')";
}
$sql .= implode(',', $queryArray);
Just change j
to your desired value.
Upvotes: 0
Reputation: 12401
IN can be used but I guess the ids that you are inserting are dynamic (can be from another table) so you may use
SELECT * FROM tagcloud
WHERE usertag_tag in (select id from the_other_table)
if not then this is okay
SELECT * FROM tagcloud
WHERE usertag_tag in (1,2,3,4)
Upvotes: 0
Reputation: 537
You should prepare a list and make a select from it:
$tags = array( 1, 2, 3, 4 );
$query = "SELECT * FROM tagcloud WHERE usertag_tag IN (" . implode( ',', $tags ) . ")";
Upvotes: 0
Reputation: 442
Something like SELECT * FROM tagcloud WHERE usertag_tag in (1, 2, 3, 4).
Upvotes: 0
Reputation: 1385
You can use the IN statement:
SELECT * FROM tagcloud WHERE user_tag IN(1,2,3,4)
Upvotes: 0