Could somebody tell me what am I doing wrong here?
$query = "SELECT *
FROM routes
WHERE econ <> maxecon
ORDER BY `id`;";
Without the WHERE, it works fine so I know that's the problem.
I want to select data where the column econ is NOT equal to maxecon. Is this even possible at this stage or do I have to take it all and chop it out in a loop?
Does replacing <> with != work?
$query = "SELECT * FROM routes WHERE econ != maxecon ORDER BY `id`";
Also, you don't need to include the ending semi-colon in your sql statement.
What you've posted looks ok. Have you definitely got examples where the two values are different? ( look at the results of your query without the WHERE ).
Also, are the fields nullable? Been a long time since I've used MySQL, but on SQL Server, items are not comparable with the <> operator if one of the operands is NULL.
Try :-
SELECT * FROM routes
WHERE
( econ <> maxecon )
OR ( econ IS NULL AND maxecon IS NOT NULL )
OR ( econ IS NOT NULL AND maxecon IS NULL )
ORDER BY id;
Quoted for your implementation language, of course.
I would try != instead of <>.
Your query should be:
$query = "SELECT * FROM routes WHERE econ != maxecon ORDER BY `id`";
Of course, if econ is equal to maxecon then you will get no results, maybe you could post some sample data from the table?
Possible problems:
You may possibly need to backquote the names econ and maxecon.
You may not have any data that meets the critieria.
You may have data that meets the critieria, but only in cases where one column is NULL, and these won't be found by <>.
If the your problem is the last one, try NOT <=>, which is the negation of a special "NULL safe equals" operator in MySQL.
What programming language is this? Most I've used don't make you put a semicolon in the query itself...
Related
Usually the query should be like below.
SELECT * FROM customers WHERE first_name = 'alex'
Can we write like this?
SELECT * FROM customers WHERE 'alex' = first_name
Additionally, if it works, why?
This is an Oracle's condition with 3 expressions, one is column expression and the other simple expression
Both are valid expressions expr:
This works, and it does so because = goes both way, meaning: what is to the left of the equals is equals to what is to the right of the equals, and the other way around.
This works on MS-SQL-Server and I am use this will work on Oracle too - why? Why not? For the server it does not matter if he compares columnes, variables oder static values or strings. It has always avaluate both side of the '='.
Yes it will be work. Because it success for expression condition
I'm Trying to delete rows from database that don't have phone, chellphone, and email (all 3 together)
DELETE *
FROM TEST
WHERE (((TEST.EMAIL1) IS Null)
AND ((TEST.PHONE3) IS Null)
AND ((TEST.PHONE1) IS Null));
For some reason if I'm doing only any two (email1, phone1 or phone3) it works, but when I'm adding second 'AND' it stop working. Any advise please.
Honestly the WHERE clause looks okay in this example, but the '*' should be left out:
DELETE FROM TEST
WHERE (((TEST.EMAIL1) IS Null)
AND ((TEST.PHONE3) IS Null)
AND ((TEST.PHONE1) IS Null));
If you have trouble after you delete the asterisk, re-copy-paste it back in, and we can see if there's a problem with parentheses or something. But the above bracketing looks okay (even if not necessary).
I'm not sure what is going on with your case but I've cleaned up your statement a bit, as you shouldn't need all those () as far as I know and DELETE doesn't require the * as it should delete anything that matches your criteria. Try it out and let me know!
DELETE
FROM TEST
WHERE EMAIL1 IS Null
AND PHONE3 IS Null
AND PHONE1 IS Null
DELETE *
FROM test
WHERE (((test.EMAIL1) ="") OR ((test.EMAIL1) IS NULL))
AND (((test.PHONE1) = "") OR ((test.phone1) IS NULL))
AND (((test.PHONE3) ="") OR ((test.phone3) IS NULL));
Worked for me. Thanks to all..
With all ANDs the parenthesis is not really necessary
DELETE
FROM TEST
WHERE TEST.EMAIL1 IS Null
AND TEST.PHONE3 IS Null
AND TEST.PHONE1 IS Null;
however, ensure that the values you expect to get deleted actually contain NULL and not something like empty string or the literal value of null.
You can check what information will be deleted by changing your statement to a select statement instead:
SELECT *
FROM TEST
WHERE TEST.EMAIL1 IS Null
AND TEST.PHONE3 IS Null
AND TEST.PHONE1 IS Null;
Based on your own answer to your question you were running into empty strings, not nulls. Another way to write what you wrote and to avoid ORs would be:
SELECT *
FROM TEST
WHERE isnull(TEST.EMAIL1, '') <> ''
AND isnull(TEST.PHONE3, '') <> ''
AND isnull(TEST.PHONE1, '') <> '';
In the above we're stating that any null test.email1s we encounter, treat as an empty string then check that that values is not an empty string.
So basically - if any of those three fields are null OR empty string. Same as your answer, just another way to write it.
You likely have no rows where all three are null. Check your data.
I am in a situation where I need to return results if some conditions on the string/character are met.
For example: to return only the names that contain 'F' character from the Person table.
How to create an SQL query based on such conditions? Is there any link to a documentation that explains how can SQL perform such queries?
Thanks in advance
The most basic approach is to use LIKE operator:
-- name starts with 'F'
SELECT * FROM person WHERE name LIKE 'F%'
-- name contains 'F'
SELECT * FROM person WHERE name LIKE '%F%'
(% is a wildcard)
Most RDBMS offer string operations which are able to perform that required task in one way or the other.
In MySQL you might use INSTR:
SELECT *
FROM yourtable
WHERE INSTR(Person, 'F') > 0;
In Oracle, this can be done, too.
In PostgreSQL, you can use STRPOS:
SELECT *
FROM yourtable
WHERE strpos(Person, 'F') > 0;
Usually there are several approaches to solve this, many would choose the LIKE operator. For more details, please refer to the documentation of the RDBMS of your choice.
Update
As requested by the questioner a few words about the LIKE operator, which are used not only in MySQL or Oracle, but in other RDBMS, too.
The use of LIKE will in some cases make your RDBMS try to use an index, it usually does not not try to do so if you use a string functions.
Example:
SELECT *
FROM yourtable
WHERE Person LIKE 'F%';
The query may look like this:
SELECT * FROM Person WHERE FirstName LIKE '%F%' OR LastName LIKE '%F%'
I use this pattern for optional filter paramaters in my SQL Stored Procedures
AND (
#OptionalParam IS NULL OR
(
Id = #OptionalParam
)
)
However the OR is not a friend of the query optimizer. Is there a more efficient way to do this without using dynamic SQL
You can try using COALESCE. Not sure if it will be more efficient.
AND Id = Coalesce(#OptionalParam, Id)
This will not work if Id itself is null and you are using ANSI nulls.
AND ID = ISNULL(#OptionalParam, ID)
or you if you had multiple optional parameters can use
AND ID = COALESCE(#OptionalParam1, #OptionalParam2, ID)
This is definitely faster than using an OR statement.
Like the other answerer mentioned, this will not work if the ID column is null (but then again, the original statement wouldn't either).
You could try:
AND Id = CASE WHEN #OptionalParam IS NULL THEN Id ELSE NULL END
I doubt this will optimize much better, but there's no OR in it.
Alternatively, you could break your query apart into two components -- one with just an #OptionalParam IS NULL test and another with an Id = #OptionalParam test, then UNION them together. Depending on your data topology this might yield better results. It could also be significantly worse.
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
I'm using a stored procedure in MySQL, with a CASE statement.
In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.
So far I've managed to do so using something like:
Select NULL From users Where False
But I have to name an existing table, like 'users' in this example.
It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.
I've tried Select NULL Where False but it doesn't work.
Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.
There's a dummy-table in MySQL called 'dual', which you should be able to use.
select
1
from
dual
where
false
This will always give you an empty result.
This should work on most DBs, tested on Postgres and Netezza:
SELECT NULL LIMIT 0;
T-SQL (MSSQL):
SELECT Top 0 1;
How about
SELECT * FROM (SELECT 1) AS TBL WHERE 2=3
Checked in myphp, and it also works in sqlite and probably in any other db engine.
This will probably work across all databases.
SELECT * FROM (SELECT NULL AS col0) AS inner0 WHERE col0 IS NOT NULL;
SELECT TOP 0 * FROM [dbo].[TableName]
This is a reasonable approach to constant scan operator.
SELECT NULL WHERE FALSE;
it works in postgresql ,mysql, subquery in mysql.
How about this?
SELECT 'MyName' AS EmptyColumn
FROM dual
WHERE 'Me' = 'Funny'
SELECT * FROM (SELECT NULL) WHERE 0
In PostgreSQL a simple
SELECT;
works. You won't even get any columns labeled 'unknown'.
Note however, it still says 1 row retrieved.