I know in other languages you can use something (usually a !) to denote the opposite of whatever follows. Is there a way to do something like this in MySQL?
For example, I have this query to select everything that isn't null or blank:
select * from `invoices` where `Notes`>'';
But is there a way way to do the opposite? Because currently I only know that you can rewrite the query such as this
select * from `invoices` where ifnull(`Notes`,'')='';
But this removes the opportunity for indexes on the Notes column or this way
select * from `invoices` where (`Notes` is null or `Notes`='');
But that one is a lot longer than something like
select * from `invoices` where !`Notes`>'';
which would be ideal.
SQL has a not operator:
SELECT * FROM `invoices` WHERE NOT (`Notes`>'');
-- Here -----------------------^
Related
I just wanted to simply count using where conditional and yet this query ask me for parameter instead of automatically execute the query
SELECT COUNT(ActDiscDischargingPort) from(
SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable) WHERE SchLoadVessel LIKE "XB24 - MV. MEMPHIS" AND SchLoadVoyageNo LIKE "0019";
What is the proper way of writing this query?
Found the answer, turn out the query has to be like this
SELECT COUNT(ActDiscDischargingPort) from ( SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable WHERE SchLoadVoyageNo LIKE "XB24 - MV. MEMPHIS" AND SchLoadVessel LIKE "0019" )
Every derived table must have its own alias.
The correct syntax would be
SELECT
COUNT(ActDiscDischargingPort)
from(
SELECT
DISTINCT ActDiscDischargingPort
FROM
SelisihLoadVSActualLoadTable
) AS T
WHERE
SchLoadVessel LIKE "XB24 - MV. MEMPHIS"
AND SchLoadVoyageNo LIKE "0019";
You can further speed up your query by optimizing it a bit.
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);
Is it possible to make a query that changes the where clause acording to some condition? For instance I want to select * from table1 where data is 19/July/2016 but if field id is null then do nothing, else compare id to something else. Like the query bellow?
Select * from table1 where date="2016-07-19" if(isnull(id),"",and id=(select * from ...))
Yes. This should be possible.
If we assume that date and id are references to columns in (the unfortunately named) table table1, if I'm understanding what you are attempting to achieve, we could write a query like this:
SELECT t.id
, t.date
, t....
FROM table1 t
WHERE t.date='2016-07-19'
AND ( t.id IS NULL
OR t.id IN ( SELECT expr FROM ... )
)
It would also be possible to incorporate the MySQL IF() and IFNULL() functions, if there's some requirement to do that.
As far as dynamically changing the text of the SQL statement after the statement is submitted to the database, no, that's not possible. Any dynamic changes to the SQL text would need to be done when the SQL statement is generated, before it is submitted to the database.
My personal preference would be to use a join operation rather than a IN (subquery) predicate.
I think you're trying too hard. If id is NULL that's equivalent to having a FALSE in the where clause. So:
Select * from table1 where date="2016-07-19" and id=(select * from ...)
Should only match the records you want. If id is NULL you get nothing.
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.
I have a mysql query, something like this:
SELECT users*100 as totalusers, totalusers*90 AS totalsalerys FROM table
As you can see I want to reuse the totalusers when calculating totalsalerys so I son't have to write the same code again. That dosen't seem to work in mysql, is there another easy way to do this?
My query is just an example, change the *100 and *90 to some very long formula and you might see what i mean..
SELECT (#r := users * 100) as totalusers,
#r * 90 AS totalsalerys
FROM table
You can also use a subquery as #Tom Ritter advices, but it's not friendly to ORDER BY ... LIMIT ... clause.
In MySQL, all results of the inner subquery will be fetched before applying any filters in the outer subquery.
I believe you would have to copy/paste the formula or use a subquery. The below would work in T-SQL, but I imagine it'd work in MySQL as well since it's pretty simple.
SELECT
x.totalusers, x.totalusers*90 AS totalsalerys
FROM (
SELECT users*100 as totalusers FROM table
) x