how can i define 2x WHERE - mysql

current query:
SELECT * FROM `invoices2` WHERE `status` = 2
I need to select status 1 and status 2, how can I do it?
I already tried things like 2 AND 1 or WHERE 'status' = 2 AND WHERE 'status' = 1
Thank you guys!

All you need is:
WHERE status IN (1, 2)
To be clear, this is equivalent to:
WHERE status = 1 OR status = 2;
You can have very complex expressions in the WHERE clause.

You can also do:
SELECT * FROM `invoices2` WHERE `status` = 1 OR `status` = 2
In single query you always have only one WHERE keyword and then you can extend it by adding OR, AND, NOT.

If they are in different rows you need and for more Status values its own extsts
SELECT * FROM `invoices2`
WHERE `status` = 2 AND EXISTS (SELECT 1 FROM `invoices2` WHERE `status` = 1)

Related

Is there a way to make an AND operation over a column of TINYINT(1) in MYSQL?

I got the following table and I need to return 1 if all rows have disponibilidad = 1
The following QUERY works just fine, but i was looking for a more efficient way of doing it.
QUERY:
SELECT IF(AVG(disponibilidad) < 1, 0, 1) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1;
RESULT:
As I see it, with an 'AND' it would be far more efficient, since it wouldn't have to do AVG().
MySql does not support a boolean AND aggregate function like Postgresql's bool_and.
Why not a simple MIN():
SELECT MIN(disponibilidad) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1;
This will return 1 only if all values of the column are 1 (provided the column is not nullable) and 0 if there is at least one row with 0.
How about something like
SELECT IF(COUNT(*)>0,0,1) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1
AND disponibilidad <> 1
so that if there are any rows where disponibilidad is not 1, you output 0, otherwise if it's zero (so all disponibilidad values are 1) you output 1?

get mysql data where column has 2 values

I have the following mysql query:
SELECT * FROM `notifications`
WHERE `receiverUserID` = 3 AND `status` = 0 AND `typ` = 1 OR `typ` = 2
the result:
But my query is not correct.
The result should show me only data where typ = 1 OR 2, status = 0 and the receiverUserID = 3
the row where receiverUserID = 2 should not be shown.
Where is my mistake ?
You need to shorten down the scope of OR operator by using parenthesis. So your query should be
SELECT * FROM notifications
WHERE receiverUserID = 3 AND status = 0 AND (typ = 1 OR typ = 2 );
Edit 1 (Helpful comment by #jBuchholz)
Here is a list of operator precedences in MySQL dev.mysql.com/doc/refman/8.0/en/operator-precedence.html AND has higher precedence than OR and is therefore executed earlier (like multiplication is executed before addition).
Here is what your query was actually doing:
SELECT * FROM
notifications
WHERE (receiverUserID = 3 AND status = 0 AND typ = 1) OR typ = 2;
This is due to that AND takes greater precedence than OR. This explains why all those typ = 2 records appear in your result set.
You need to use parentheses to enforce the and/or logic you have in mind:
SELECT * FROM
notifications
WHERE receiverUserID = 3 AND status = 0 AND (typ = 1 OR typ = 2);
Note that had used WHERE IN (...) syntax this would have been a moot point:
SELECT * FROM
notifications
WHERE receiverUserID = 3 AND status = 0 AND typ IN (1, 2);

Mysql query returns wrong data with where clause

I am using the following query to get data from mysql database and I get wrong data. I want to get all data with the cart_Status of 2 or 3 which have the view_Status of 1:
SELECT * FROM `cart` WHERE `view_Status` = 1 AND cart_Status = 2 OR `cart_Status` = 3
This is how my data structure and table looks like:
But in result, it returns something regardless of view_Status = 1 which is not my target.
it returns :
Of course, it should not return anything! But, it does!
This is about operator precendence.
Your query evaluates as
SELECT * FROM `cart` WHERE (`view_Status` = 1 AND cart_Status = 2) OR `cart_Status` = 3
You should to add parentheses:
SELECT * FROM `cart` WHERE `view_Status` = 1 AND (cart_Status = 2 OR `cart_Status` = 3)
SELECT * FROM `cart` WHERE `view_Status` = 1 AND (cart_Status = 2 OR `cart_Status` = 3)
or better
SELECT * FROM `cart` WHERE `view_Status` = 1 AND cart_Status in (2, 3);
You appear to be learning SQL. Use parentheses in the WHERE clause, particularly when you mix AND and OR.
However, in your case, IN is a better solution:
SELECT c.*
FROM `cart` c
WHERE c.view_Status = 1 AND cart_Status IN (2, 3);
It's a problem with operators precedence. Typically AND is executed before OR in programming languages (think of AND as of multiplication of bits, and of OR as of addition of bits and precedence becomes familiar). So, your condition:
`view_Status` = 1 AND cart_Status = 2 OR `cart_Status` = 3
is parsed like this:
(`view_Status` = 1 AND cart_Status = 2) OR `cart_Status` = 3
which results in all rows with specific cart_Status to be selected. You have to add parenthesis around the second clause:
`view_Status` = 1 AND (cart_Status = 2 OR `cart_Status` = 3)
or, even shorter:
`view_Status` = 1 AND cart_Status IN (2, 3)

Update table with subquery works on mysql but error on oracle

I have table named TABLE_A looks like this :
ID DATA VALUE LM
---------------------------------
1 7 9 NULL
2 10 5 NULL
3 4 7 NULL
This is not actually my table, i use this to shorten my question.
Now I want to update table_a with subquery.
This is my query :
UPDATE TABLE_A,
(SELECT VALUE AS VAL FROM TABLE_A WHERE ID = 2) AS TEMP
SET TABLE_A.LM = TABLE_A.VALUE + TEMP.VAL
WHERE TABLE_A.ID = 1
This query works on Mysql but in oracle I got error :
[Err] ORA-00971: missing SET keyword
EDIT :
This is my table [SDM_ABSENSI] :
PERIODE TGL_IN TGL_OUT IN OUT LM TL
------------------------------------------------------------------
20141011 11/01/2014 11/01/2014 08:00 17:00 NULL NULL
20141012 12/01/2014 13/01/2014 22:00 07:30 NULL NULL
20141013 13/01/2014 13/01/2014 08:00 17:00 NULL NULL
My query :
UPDATE SDM_ABSENSI A
(
SELECT PERIODE, TGL_IN, TGL_OUT, IN, OUT,
TO_DATE(TO_CHAR(TGL_IN,'YYYY-MM-DD')||' '||IN,'YYYY-MM-DD hh24:mi') AS MASUK,
TO_DATE(TO_CHAR(TGL_OUT,'YYYY-MM-DD')||' '||OUT,'YYYY-MM-DD hh24:mi') AS KELUAR
FROM SDM_ABSENSI
WHERE SUBSTR(PERIODE,0,6) = '201410'
)ABSEN
SET A.LM = (24*60) * (ABSEN.KELUAR - ABSEN.MASUK),
A.TL = CASE WHEN (24*60) * (ABSEN.KELUAR - ABSEN.MASUK) < 0
THEN 0 ELSE (24*60) * (ABSEN.KELUAR - ABSEN.MASUK)
END
WHERE SUBSTR(A.PERIODE,0,6) = '201410'
AND A.PERIODE = ABSEN.PERIODE
And i got error :
[Err] ORA-00971: missing SET keyword
Please help,
Thanks in advance
Oracle does not support Update from Join Syntax. Instead you can use Merge. Try this.
MERGE
INTO SDM_ABSENSI
USING (
SELECT PERIODE, TGL_IN, TGL_OUT, IN, OUT,
TO_DATE(To_char(TGL_IN,'YYYY-MM-DD')||' '||IN,'YYYY-MM-DD hh24:mi') AS MASUK,
TO_DATE(TO_CHAR(TGL_OUT,'YYYY-MM-DD')||' '||OUT,'YYYY-MM-DD hh24:mi') AS KELUAR
FROM SDM_ABSENSI
WHERE SUBSTR(PERIODE,0,6) = '201410'
) ABSEN
ON SDM_ABSENSI.PERIODE = ABSEN.PERIODE
WHEN MATCHED THEN
UPDATE
SET SDM_ABSENSI.LM = ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK ),
SDM_ABSENSI.TL = CASE
WHEN ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK ) < 0 THEN 0
ELSE ( 24 * 60 ) * ( ABSEN.KELUAR - ABSEN.MASUK )
END
I don't think you can write such a subquery in Oracle. You should maybe checkout the update statement as its defined in the oracle documentation, here http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/update_statement.htm
Having said that, what do you really want to do here? What value and under which conditions do you want to assign to the column LM?
That query doesn't look so good, in my opinion. You're trying to build a temporary table from the data stored in table_a and update that same table_a with values from that temporal table... but how? With the maximum of the temporal table? With the value of the same register when the condition is met?
I don't see how that query could work in MySQL either to be honest.
To sum up, could you provide additional info?
[EDIT] Just saw the modification in the question. You can remove the subquery from where it is and place it in the where statement...
UPDATE TABLE_A
SET TABLE_A.LM = TABLE_A.VALUE + (SELECT VALUE AS VAL FROM TABLE_A WHERE ID = 2)
WHERE TABLE_A.ID = 1

Mysql Case - 2 conditions need same update value

I am trying to use a mysql case query to update multiple rows in a table. I have some cases which need to update the row with the same value. I was wondering whether it is possible to put all of these into one case or whether I have to create a new 'WHEN' for each?
Below is an example of what I am trying to accomplish but it obviously isn't the correct way to do this because I get an error.
UPDATE `groups` SET `status` = CASE `group_id`
WHEN 32 OR WHEN 33 THEN '1'
WHEN 31 THEN '2'
END
Is it possible to do something like that?
Thanks
I think you want
UPDATE groups SET status = CASE
WHEN group_id = 32 OR group_id = 33 THEN '1'
WHEN group_id = 31 THEN '2'
END
Edit You can use operators like BETWEEN. For example
UPDATE groups SET status = CASE
WHEN group_id BETWEEN 32 AND 33 THEN '1'
WHEN group_id BETWEEN 30 AND 31 THEN '2'
END
TRY
UPDATE `tablename` SET `status`= IF('group_id=31',2,1)
EDIT
UPDATE tableName SET `status` = IF( group_id IN (31, 32), 2, 1 ) WHERE section_id=1
OR
UPDATE tableName SET `status` = IF( group_id ANY (31, 32), 2, 1 ) WHERE section_id=1
running successfully on my table..what error u facing??
other syntax
UPDATE `tableName` SET `group_id` = CASE
WHEN group_id IN (31,32) THEN 1
WHEN group_id IN (33,34) THEN 2
END