mysql SELECT WHERE LIKE multiple conditions - mysql

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);

Related

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

select all columns, simultaneously doing a ifnull on one

I want to be able to do a select on all columns, displaying a 0 (for a few of them) if null, without having to write each of the columns' names in the statement.
All I could think of is something like this:
SELECT *, IFNULL(`nullable_col1`, 0) FROM `my_table`;
What's the right way to do this?
No there is no way. You have to use the IFNULL function on each column which you want to have the value for.
One thing which you can do is that, you can simply select the value for all the columns which are not NULL(but I am not sure if that is what you want)
SELECT * FROM my_table WHERE (nullable_col1 AND nullable_col2 AND nullable_col2) IS NOT NULL
So this will select only columns which are not NULL.

MySQL returns all rows when field=0 from SECOND Select query

This case is similar to: S.O Question; mySQL returns all rows when field=0, and the Accepted answer was a very simple trick, to souround the ZERO with single quotes
FROM:
SELECT * FROM table WHERE email=0
TO:
SELECT * FROM table WHERE email='0'
However, my case is slightly different in that my Query is something like:
SELECT * FROM table WHERE email=(
SELECT my_column_value FROM myTable WHERE my_column_value=0 AND user_id =15 LIMIT 1 )
Which in a sense, becomes like simply saying: SELECT * FROM table WHERE email=0, but now with a Second Query.
PLEASE NOTE: It is a MUST that I use the SECOND QUERY.
When I tried: SELECT * FROM table WHERE email='( SELECT my_column_value FROM myTable WHERE my_column_value=0 LIMIT 1 )' (Notice the Single Quotes on the second query)
MySql SCREAMED Errors near '(.
How can this be achieved
Any Suggestion is highly honored
EDIT1: For a visual perspective of the Query
See the STEN_TB here: http://snag.gy/Rq8dq.jpg
Now, the main aim is to get the sten_h where rawscore_h = 0;
The CURRENT QUERY as a whole.
SELECT sten_h
FROM sten_tb
WHERE rawscore_h = (
SELECT `for_print_stens_rowscore`
FROM `for_print_stens_tb`
WHERE `for_print_stens_student_id` =3
AND `for_print_stens_factor_name` = 'Factor H' )
The result of the Second Query can be any number including ZERO.
Any number from >=1 Works and returns a single corresponding value from sten_h. Only =0 does not Work, it returns all rows
That's the issue.
CORRECT ANSWER OR SOLUTION FOR THIS
Just in case someone ends up in this paradox, the Accepted answer has it all.
SEE STEN_TB: http://snag.gy/Rq8dq.jpg
SEE The desired Query result here: http://snag.gy/wa4yA.jpg
I believe your issue is with implicit datatype conversions. You can make those datatype conversions explicit, to gain control.
(The "trick" with wrapping a literal 0 in single quotes, that makes the literal a string literal, rather than a numeric.)
In the more general case, you can use a CAST or CONVERT function to explicitly specify a datatype conversion. You can use an expression in place of a column name, wherever you need to...
For example, to get the value returned by my_column_value to match the datatype of the email column, assuming email is character type, something like:
... email = (SELECT CONVERT(my_column_value,CHAR(255)) FROM myTable WHERE ...
or, to get the a literal integer value to be a string value:
... FROM myTable WHERE my_column_value = CONVERT(0,CHAR(30)) ...
If email and my_column_value are just indicating true or false then they should almost certainly be both BIT NOT NULL or other two-value type that your schema uses for booleans. (Your ORM may use a particular one.) Casting is frequently a hack made necessary by a poor design.
If it should be a particular user then you shouldn't use LIMIT because tables are unordered and that doesn't return a particular user. Explain in your question what your query is supposed to return including exactly what you mean by "15th".
(Having all those similar columns is bad design: rawscore_a, sten_a, rawscore_b, sten_b,... . Use a table with two columns: rawscore, sten.)

having issue using and/or in mysql query

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)

Which logical operator do I want to use in this mysql statement?

Which is a better way to select ans and quest from the table?
SELECT * FROM tablename WHERE option='ans' OR option='quest'";
OR
SELECT * FROM tablename WHERE option='ans' AND option='quest'";
Thanks so much!
Your second statement is not going to return any results. A record cannot have option=ans and option=quest at the same time.
It's not a question of a 'better' way - only the first one works. Even though you want option=ans and option=quest in your results set, the WHERE clause is executed once per row. So you're telling MySQL "give me a row where option=quest and option=ans" i.e. option is two values at once, which is impossible. You actually want to get rows where either is true, which is why you use OR.
I think this reads better:
SELECT * FROM tablename WHERE option IN('ans','quest');
If the rows that represent question have option set to 'quest' and rows with answer have option set to 'ans' then you should use option='ans' OR option='quest'";. Also a row cannot represent both question and answer so using AND will not select any rows.
This select will return all rows whos options is ans or quest
SELECT * FROM tablename WHERE option='ans' OR option='quest'";
This select on the other hand will not return any rows since a column has only one of those values
SELECT * FROM tablename WHERE option='ans' AND option='quest'";
Use this if you want your search to return both answers and questions:
SELECT * FROM tablename WHERE option='ans' OR option='quest';
It can also be written:
SELECT * FROM tablename WHERE option in ('ans','quest');