variable
07-26-2005, 02:09 AM
i need to select anything from a mysql table where category= one of several things
ie something like:
where cat=1 or cat=2 or cat=fun
something like that if it makes sense
Elliot A
07-26-2005, 02:35 AM
SELECT * FROM table_name WHERE cat = # or cat = #
You can use ( and ) in SQL statements like: (cat = 'a' AND cat = 'b') OR (cat = 'c' AND cat = 'd')
Also, it depends on what type of data you are using in the statement where the # are. For example, if the column contains int's, just go cat = 1, otherwise if the column is string data etc use cat = 'text here'
error404
07-26-2005, 02:58 AM
It's more efficient and easier to read to use the IN keyword:
SELECT * FROM table WHERE cat IN (1,2,'fun');
This does exactly what you propose: it selects columns where the 'cat' column matches a value 'IN' the list you provide.
variable
07-26-2005, 03:16 AM
just saw the 'in' deal, i am gonna stick with or code now that i have it working, it seems easy enough.
thanks much both!
hiryuu
07-26-2005, 03:41 AM
Although OR code may work, keep in mind that it severely hampers your ability to index the query, which is crucial for performance.
variable
07-26-2005, 10:39 AM
so 'in' is going to be faster than 'or'?
i could not get in to work i tried:
WHERE user='$user' AND cat IN ( 1, 2, 'fun')
and it did not work.
probonic
07-26-2005, 11:11 AM
Looks like the problem there is you're mixing numbers and a string. try:
WHERE user='$user' AND cat IN ('1','2','fun')
hiryuu
07-26-2005, 03:39 PM
Yeah, it may not like the mixed types. An actual error message would help, though.
PerfTuner
07-26-2005, 05:33 PM
Although OR code may work, keep in mind that it severely hampers your ability to index the query, which is crucial for performance.
Actually recent versions of MySQL Query Optimizer (starting from 4.0 I believe) are able to detect ORs and use range select when possible, just like when using IN(...).
variable
07-29-2005, 12:16 AM
oh, cool then i will stick