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);
Related
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)
I have many tables with the same structure for each of my costumers.
All information are distinct for each table.
For some reason, I need to create some temporary table with the same structure with 200 values everytime I run.
So let's assume I have 3 tables.
Costumer1, Costumer2, Costumer3.
All those tables have id, user_name, contact_name, contact_email, sent1, sent2, sent3, sent4, status.
I need some query to put inside costumer_tmp, only 200 values in total, from all those 3 tables, everytime I run the script. And everytime I run cant repeat the last values I got before.
So for example:
Costumer1
id = 29
user_name = test1
contact_name = contact1
contact_email = contact1#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
Costumer2
id = 37
user_name = test2
contact_name = contact123
contact_email = contact123#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
Costumer3
id = 87
user_name = test3
contact_name = contact231
contact_email = contact231#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
How to Insert on costumer_tmp only 2 records of those 3 and next time I run the script don't repeat those 2 records, just insert only 1 record remaining.
Your requirement is a bit weird, but if I understand weel, how about something like : (not tested)
insert into costumer_tmp
select * from Costumer1 one where one.id not in (select id from costumer_tmp)
union all
select * from Costumer2 two where two.id not in (select id from costumer_tmp)
union all
select * from Costumer3 three where three.id not in (select id from costumer_tmp) LIMIT 200
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)
I have
Array ( [406] => 1 [407] => 3 [408] => 2 [409] => 7 [410] => 1 )
run as mysql query
UPDATE counter SET total = 1 WHERE id = 406;
UPDATE counter SET total = 3 WHERE id = 407;
UPDATE counter SET total = 2 WHERE id = 408;
UPDATE counter SET total = 7 WHERE id = 409;
UPDATE counter SET total = 1 WHERE id = 410;
I can only optimized query above by grouping same total value as below:
UPDATE counter
SET total = 1
WHERE name IN (406, 410);
Is there any way to optimize it better, rather than execute (loop) the update query one by one.
You need this:
UPDATE counter SET total = CASE
WHEN id = 406 THEN 1
WHEN id = 407 THEN 3
WHEN id = 408 THEN 2
WHEN id = 409 THEN 7
WHEN id = 410 THEN 1
END
you can use key value pair to update like below
UPDATE counter SET total = '".$value."' WHERE id = '".$key."';
suppose the user input
mysite.com/profile?identity=1
mysite.com/profile?identity=dinodsja
mysite.com/profile?identity=1a
getting the value
$identity = $_GET['identity']; // identity can be user_id or user_name
and i have a simple select query:
SELECT * FROM lb_users WHERE (user_id = 'dinodsja' OR user_name = 'dinodsja') AND user_status = 1
and it works fine. but the problem is:
SELECT * FROM lb_users WHERE (user_id = '1a' OR user_name = '1a') AND user_status = 1
when I execute this query it also returns the result without satisfying the condition.
Table structure:
user_id bigint(25)
user_name varchar(50) utf8_general_ci
**
-> Is this a MySQL Bug ?
-> How can we avoid this ?
-> What will be the query ?
**
The reason for that is because the data type of the column user_ID is integer.
MySQL silently drops any trailing NON-Number (and anything that follows within) in the value and that is why 1a is equal to 1 since a will be remove in the value.
SQLFiddle Demo
I do remember having a similar problem long ago.
First some background: This is not a bug. It is actually a feature. Ok, it's one that might lead to such unexpected behaviour, but MySQL is thereby very tolerant w.r.t. user inputs, respective select queries:
mysql> SELECT 'a' = 'a ';
-> 1
mysql> SELECT 'A' = 'a';
-> 1
Therefore, with implicit type conversion, the result of, e.g, '1a' in INTEGER is 1, but also:
mysql> SELECT 0 = 'x6';
-> 1
mysql> SELECT 1 = ' 1';
-> 1
mysql> SELECT 1 = ' 1a';
-> 1
This feature is also implemented in other not statically typed languages. PHP, for instance, calls this type juggling. See the PHP String conversion rules and this example from the documentation:
<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
?>
See JavaScript:
<script>
document.write(parseInt("40 years") + "<br>");
</script>
=> 40
Nevertheless, the solution to your problem is pretty easy: Just cast the integer to a char and do the comparison then:
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = '1' OR user_name = '1')
-> 1
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = '1a' OR user_name = '1a')
-> 0
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = 'dinodsja' OR user_name = 'dinodsja')
-> 1
I made a fiddle for everyone to try it out: http://sqlfiddle.com/#!2/c2835/14/0
Hope that helps,
-Hannes
According to your previous message
its a user input for profile. user can provide user_id or user_name.
so the input is valid. but no data. – DBK Mar 30 at 6:42
I'd recommend testing to see if its an integer and only search the user ID if it's an integer. It's really more of a workaround for mySQL not handling a STRING to INT comparison, but it should work.
declare #InputVar varchar(10)
set #InputVar = '1a'
SELECT *
FROM lb_users
WHERE
(case when isnumeric(#InputVar) = 1 then
case when (user_id = #InputVar OR user_name = #InputVar) then 1 else 0 end
else
case when user_name = #InputVar then 1 else 0 end
end =1 )
And
user_status = 1
When dealing with strings I would use 'LIKE' instead of '=' to avoid this silent type conversion madness. LIKE is made to work with strings so why not use it.
SELECT * FROM lb_users WHERE (user_id = '1a' OR user_name = '1a') AND user_status = 1
you get 1 result if you change '1a' to 1a you get this:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1a LIMIT 0, 30' at line 1
This is not a bug, take a look at http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html
hope this helps
I think you cannot duplicate a primary key and an ID, I test that one and i come up with a running data..did you set the user_id with its attributes like:
user_id bigint(50) auto_increment primary key
this is not a mysql error.