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
Related
Name
UID
Late
Tin
ABC
0
Bob
ABC
0
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
This query returns nothing for me and only works when I want one name.
I could use the SELECT * but for this case I would like to call specific names in the query?
For this use In clause.
SELECT * FROM logs WHERE Name IN ('Tin', 'Feryal');
You can also use or clause
SELECT * FROM logs WHERE Name='Tin' OR Name='Feryal'
To add on to Amit Verma's answer.
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
Reading that SQL statement out loud, it sounds like this:
I want to select all the rows from logs where the name is equal to Tin AND the name is equal to Feryal.
You can quickly see from that statement, the reason why 0 rows are returned, it's because that is impossible! You cannot have somebody named both Tin and Feryal at the same time unless they are some bizarre super-positional being and the datatype in the table somehow allows for that.
Amit covers the rest.
I received wonderful help in answering a question yesterday that lead me to successfully use SELECT COALESCE in a MySQL database to replace a nested excel formula. What i'm now struggling with is using the SELECT COALESCE in conjunction with using the SELECT command in the same query. All my columns are coming from the same table (best_estimate) but what I would like to do is:
SELECT 'OFS_ID' from best_estimate
and also perform the following:
SELECT COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
so the end result will result in two columns -
OFS_ID and MY_VALUE
I'm just not sure how to join these two different commands - I spent three hours working on it yesterday and I keep getting syntax errors saying that I can't use SELECT in that place in the query.
Thanks in advance for the help!
Have you tried
SELECT OFS_ID,COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value FROM best_estimate
Try this:-
SELECT `OFS_ID`, COALESCE(FINAL_TOTAL, INITIAL_TOTAL, CDSI_TOTAL, JCE_TOTAL, 0 ) AS my_value
FROM best_estimate
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.
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.
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');