having issue using and/or in mysql query - mysql

I'm having an issue thinking how to write the query i need.
I'll give you an example of what i'm trying to do..
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30||40
I need to select entries where clause1 must be exactly 10 and clause2 must be exactly 20, but clause3 can be 30 or 40 but have to be exactly 30 or exactly 40.
so it would select the entry if it were 10,20,30 or 10,20,40
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40 is incorrect though isn't it.
The numbers and clauses are only for example.
Thanks.

You've got to use parentheses 'round your last condition and use two times clause3=:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 || clause3=40)
or
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3 IN (30, 40)
Note
I would recommend to use the operator OR instead of ||, because in other SQL dialects (i.e. Oracle, Postgres) || is the concatenation operator. OR is standard SQL. So the first statement would better be written:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 OR clause3=40)
Explanation
The operator AND has got higher precedence than OR. Without parentheses the expression the following two statements would be evaluated the same
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40
SELECT * FROM table WHERE (clause1=10 AND clause2=20 AND clause3=30) OR clause3=40
and that is not what you want. So either put parentheses around your OR expressions or use the simpler IN().

Use IN.
SELECT *
FROM table
WHERE clause1=10 AND clause2=20 AND
clause3 in (30,40)

Related

mysql SELECT WHERE LIKE multiple conditions

Nothing is being returned when I execute this query!
I'm trying to have multiple values for same column
SELECT * FROM `users` where Number='1212' AND Number='0921'
please, can you help?
Change the 'AND' to an 'OR'. A number (or anything else in a table column for that matter) cannot be both values at the same time
SELECT * FROM `users` where Number='1212' OR Number='0921'
Where you need rows where an element can be one of a number of values, it is common to use IN instead of repeated OR clauses, as this makes the expression clearer:
SELECT * FROM `users` where Number IN ('1212','0921');
As one of your Number values has a leading zero, I presume it is genuinely a string. You might want to name your columns better.
Or better use the IN operator, so you could filter for even more values:
SELECT * FROM `users` where Number IN ('1212', '0921');
Try this:
SELECT * FROM users where Number=1212 OR Number=0921
Change AND by OR, if you need to check equality with more values you could just use SELECT * FROM users WHERE number IN(1212, 0921, 1452, 1265);

How can I use column relative position in if statement in order by?

I run a SQL query like below in MySQL:
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 1),o,t);
It works well, but when I use column relative position in if statement, it doesn't work.
How can I use column relative position in if in ORDER BY statement?
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 0),1,2);
I'm not sure what your real confusion is. When an integer appears in an order by, then this is treated as a column number. Any other use of an integer is interpreted as an expression.
The use of column numbers has been removed from the SQL standard. Hence, its use in any particular database is not guaranteed in future releases. It is really better to use the column names.
I think you want to sort your query based on a criteria over two columns, if I'm correct, you can use something like this:
...
order by
case when (your criteria)
then column1
else column2
end;
Note: use union all instead union when you don't want to remove duplicate values as performance issue ;).

Where clause and truncating decimal points

Values in my table have a number of decimal points:
20.828292
21.9292992
...
I need to query the table to find results that match but to only 1 decimal place.
So where 20.8 would match the 20.828292.
Can I do this with SQL? How?
you can do it like
select * from table where TRUNCATE(attribute,1)=20.8
MySQL supports lots of comparison operators, like >= and <, which should be combined to get you the range you're looking for.
You can't do it by just comparing
select * from table where atribute = 20.8
In your case do a query like
select * from table where atribute >= 20.75 and atribute < 20.85
That should do it

mysql using select with an arithmetic expression

I am trying to use select in combination with an arithmetic expression
SELECT * FROM `mytable` ORDER BY (`column1` / max(`column1`)* `column2`);
The problem is that it return a single row rather than all the rows sorted base on the expression.
Any idea?
Just order by column1. Dividing by a constant doesn't change the order.
EDIT: Add your expression as another field in the select. E.g.,
SELECT *, x+y as z ORDER BY z
See https://stackoverflow.com/a/10762333/2877364 for a full example of a similar situation.

MySQL Wildcard for "=" - is there one

So,
SELECT * FROM table WHERE col LIKE '%'
will return everything. Is there a wildcard for the query
SELECT * FROM table WHERE col = '*'
Clearly * doesn't work, I just put it there to indicate where I'd like a wildcard. The column I'm selecting from contains an integer between 1 and 12, and I want to be able to select either all records with a particular number, or all records with a wildcard.
Thanks,
LIKE is basically the same as =, except LIKE lets you use wildcards.
These two queries will return the same results:
SELECT * FROM table WHERE col LIKE 'xyz';
SELECT * FROM table WHERE col='xyz';
Without a '%' in the LIKE query, it is effectively the same as '='.
If you're doing a selection on an integer column, you should consider using the IN() or BETWEEN operators. It sounds like you have two separate conditions that should be handled in your code however, rather than in the query, as your conditions dictate that you need at least two different kinds of queries.
Edit: I should clarify that LIKE and = are similar only in normal, humdrum string comparison usage. You should check the MySQL Manual for specifics on how it works, as there are situations where it's not the same (such as language sets).
If you want to select everything, why are you attaching the WHERE clause at all? Just leave it off conditionally instead of putting a wildcard into it.
The reason for using LIKE is because the = does not offer wildcard support. Otherwise there would be no reason for LIKE
SELECT * FROM table WHERE col RLIKE '.*'
i.e. regular-expression LIKE.
zombat's answer is great, but I only noticed in his answer that you are selecting integers. He mentioned IN() and BETWEEN(). Here's examples using those syntaxes, as well as some other options you have for an integer field.
SELECT * FROM table WHERE col = 1;
SELECT * FROM table WHERE col BETWEEN 1 AND 12;
SELECT * FROM table WHERE col BETWEEN 6 AND 12;
SELECT * FROM table WHERE col <= 6;
SELECT * FROM table WHERE col < 6;
SELECT * FROM table WHERE col >= 6;
SELECT * FROM table WHERE col > 6;
SELECT * FROM table WHERE col <> 6;
SELECT * FROM table WHERE col IN (1,2,5,6,10);
SELECT * FROM table WHERE col NOT IN (1,2,5,6,10);
Assuming your query is parameter driven a case statement is probably appropriate
select * from mytable
where col like case when #myvariable is null then % else myvariable end
Where #myvariable is either null if you dont want a value otherwise it would use the integer value you pass in.
I have encountered such a case while building a stored procedure for a report
Following is my solution, hope this is what you had in mind :)
set #p = "ALL";
Query:
select * from fact_orders
where
dim_country_id = if(#p is null or #p="ALL", dim_country_id, #p)
limit 10
;
If your values are in the the range (1,12) then:
select * from table where col>=5 and col<=5; //this is equal to col=5
select * from table where col>=0 and col<=12; //this is equal to col=any value
The same line can produce both effects by choosing the 2 parameters appropriately.
I faced a similar problem when I needed a single prepared statement which should work with 2 different ways , either checking for a particular value in a column or ignoring that column completely.