What is an operator in MySQL queries that covers AND and OR. So if I wanted to select a row where value1=lol OR value2=loly or both do.
this will look for one of the values or both.
SELECT * FROM my_table WHERE your_column IN ( 'lol','loly' )
With a query with the OR it'll work:
SELECT * FROM my_table WHERE my_field = 'lol' OR my_field = 'loly'
it'll give you all the results with the first or the second
As an extra, I think you could be looking for WHERE with regular expressions: Check this manual page: https://dev.mysql.com/doc/refman/5.1/en/regexp.html
Sorry, this wasn't a very good question.
I now understand that the OR operator will work with either condition met, or both.
AND only works with both.
XOR works with either one but not both.
Credit goes to the comments replying to my question.
Related
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.)
So I have joined 3 queries together using UNIONs and want to count the number of lines in the result, but it's a bit weird. It actually works, and gives the correct answer, but it doesn't assign the "AS" part correctly.
SELECT COUNT(*) FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
The outcome is correct, but instead of assigning it to "NoOfTweets" it assigns it to "Count(*)". If I remove the "AS NoOfTweets" it stops working. If I remove some brackets it stops working. I'm running low on ideas after a long day! I can post the whole code if needs be but would rather not as it's quite long and I think that bit works.
Thanks in advance, Jack.
Edit: Fixed with:
SELECT COUNT(*) NoOfTweets FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
Thanks guys :)
You aren't putting it in the correct location. The beginning of your query should look like this:
SELECT COUNT(*) AS NoOfTweets
More on Column Alias
SELECT COUNT(*) NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
or
SELECT COUNT(*) AS NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
You have to use AS exactly after the item you are counting:
SELECT COUNT(*) AS `NoOfTweets`
FROM ( ... )
Also be careful with the " you have near the end. Or maybe it comes from a longer string.
The error is Every derived table must have its own alias which is something I didn't know, so thanks for the education :)
http://sqlfiddle.com/#!9/d30f4/4
Nice of MySQL to give an explanation - I tried with MS SQL on SQLFiddle and just got Incorrect syntax near ')'. which isn't so helpful!
So, your 'NoOfTweets' is the name given to the results column, and also to the 'derived table' which is required by the SQL engine but could be a different name ... it's not returned in the results. The point of naming a derived table is in case you wish to JOIN to other tables and reference the fields in the joins.
I'm looking for a query with the lowest possible resource usage which gives no result. For example (this won't work):
SELECT 1 WHERE 0
Edit:
The goal would be to be used in an EXISTS subquery in special cases where I want no matches. (I know this is not the only possible solution, but in my environment I chose to use this one).
SELECT 1 FROM DUAL WHERE 0
will do the job; you will not even need a USE-statement before.
select '1' from tableName where 0
SELECT 1; would work but it gives a result set
You could try SELECT NULL from <any table>
A USE <database name> statement gives no result and takes up minimal resources.
What is this for exactly?
Perhaps you could do
select 1 where 1=0
I ran a query that resulted in the string '1,2,3,4'.
How can I run a second query that treats that string as a list of numbers. So I'll be able to do:
select * from tbl where name not in (1,2,3,4)
I would like an answer in pure MySQL.
Well first of all, this usually means that your database structure is not good; you should normalize your database.
However, you can do what you want, with the FIND_IN_SET function:
SELECT * FROM tbl WHERE NOT FIND_IN_SET(name, '1,2,3,4')
Use FIND_IN_SET:
select * from tbl where FIND_IN_SET(name, '1,2,3,4') = 0
Like the other answer, I would also recommend normalizing your database if at all possible. This query could be slow as it will require a scan of the table. Even if there is an index on name this query won't be able to use it efficiently.
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');