Conditional SELECT based upon IF EXISTS in MySql - mysql

There is a table users. Based upon 2 column data I need to select rows.
Let say these 2 columns are ID and ID_API.
So while fetching data, I need to search 1st if for a given value exists in ID_API column, then SELECT that row.
Else use ID in WHERE condition.
Here is What is look like
IF EXISTS (SELECT * FROM users WHERE ID_API= '12345')
BEGIN
SELECT * FROM users WHERE ID_API= '12345'
END
ELSE
BEGIN
SELECT * FROM users WHERE ID_= '12345'
END
How to manage it?

In the general case, you can look for rows that have ID_API = 12345, or rows that have ID = 12345 as long as there is no row with ID_API = 12345:
SELECT *
FROM users
WHERE ID_API = '12345' OR
ID = '12345' AND NOT EXISTS (SELECT * FROM users WHERE ID_API = '12345')
In the special case where there is only one row per user, you can do that by searching for either ID_API = 12345 or ID = 12345, then ordering results by ID_API = 12345 descending (since that will return a value of 1 if it is true) and using LIMIT 1 to restrict to 1 row.
SELECT *
FROM users
WHERE ID_API = '12345' OR ID = '12345'
ORDER BY ID_API = '12345' DESC
LIMIT 1

Related

How to select and update row, so that the same row could not be selected again by any user?

I am sending sms to mobile numbers using many apis at the same time, now I want to ensure that only one sms should be sent to one mobile number.
I am selecting a row using the following query
SELECT s_contact_name, s_contact_number, i_sms_sent_count
FROM contacts
WHERE i_sms_sent_count = 0
ORDER BY ts_time_collected DESC
LIMIT 1;
while selecting the row I want to set field i_sms_sent_count to 1 so that this row could not be selected again by any user.
One method is to do the update and then get the values. Assuming you have a contact_id column:
update contacts
set i_sms_sent_count = 1,
contact_id = (#contact_id := contact_id) -- does nothing except set the variable
where i_sms_sent_count = 0
order by ts_time_collected desc
limit 1;
Then get the information:
select *
from contacts c
where contact_id = #contact_id;
EDIT:
An alternative is:
start transaction;
select contact_id into #contact_id
from contacts
where i_sms_sent_count = 0
order by ts_time_collected desc
limit 1
for update;
update contacts
set i_sms_sent_count = 0
where contact_id = #contact_id;
commit transaction;

NodeJS, MySQL - Advanced query

"Test" table structure
id
value
itemID
I want to check if in table "Test" there is an result with itemID = '123' and value = '456' and whether it is the last added result ORDER BY id DESC, I miss something in this code:
SELECT * FROM Test WHERE itemID = '123' AND value= '456' ORDER BY id DESC LIMIT 1
Could anyone help?
check if in table Test there is an result with itemID = '123' and value = '456' and whether it is the last added result ORDER BY id DESC
Your requirement can be litteraly translated as follows:
select *
from test t
where itemID = 123 and value = 456
and not exists (
select 1
from test t1
where t1.id > t.id
)
The NOT EXISTS condition ensures that the record being selected is the latest, id-wise.
If the requirements are not satisfied, the query returns an empty resultset.
Another way to express it is to use a correlated subquery to get the latest id:
select *
from test t
where
itemID = 123
and value = 456
and id = (select max(id) from test t)
Using Sub query in where clause you can find it.
SELECT t.* FROM `Test` as t WHERE `itemID` = 123 AND `value` = 456 AND `id` =(SELECT max(`id`) FROM Test);
SELECT (SELECT value
FROM Test
WHERE itemID = '123'
ORDER BY id DESC LIMIT 1) = '456' AS it_matches;
The result will be one of these possibilities:
1 if the last "value" is 456, or
0 if the last "value" is another non-null value, or
NULL if there are no rows with ItemID = 123 or the last row's "value" column is null.

Using if() in a where clause

I need to do an if else on a where clause.
I want to do something like
Select id from table where if(client_id = 10, client_id = 10, client_id = 1) AND foo = A
In clear I need
if client_id 10 exist where foo = A return id
else
client_id 10 doesn't exist where foo = A
use client_id = 1 where foo = A
I don't think you need an IF. You could try with this query:
SELECT id FROM table WHERE client_id in (10,1) AND foo='A';
EDIT:
Query example 1:
SELECT IF(client_id=10 AND foo='A',id,'') AS '10A',
IF(client_id <> 10 AND client_id=1 AND foo='A',id,'') AS '1A'
FROM table HAVING (10A OR 1A) <> '';
Query example 2:
SELECT id
FROM table
WHERE
client_id=CASE WHEN client_id=10 AND foo='A' THEN client_id
WHEN client_id <> 10 AND client_id=1 AND foo='A' THEN client_id
END;
Query example 3:
SELECT id
FROM table
WHERE
client_id=IF(client_id=10 AND foo='A',client_id,
IF(client_id <> 10 AND client_id=1 AND foo='A',client_id,''));
The last example could be what you initially have in mind.

MYSQL Select with IF in where clause

I have a list of items in MySQL table each one has a group number & unique item number.
I'm Trying to select records that mach 2 criterias.
First I need to select record that satisfies group & item numbers. If none found i want records selected that just belongs to a group.
Only one record has to be selected either way.
SELECT *
FROM YourTable
WHERE GroupID = :group
ORDER BY ItemID = :item DESC
LIMIT 1
LIMIT 1 makes it return just one record. The ORDER BY clause makes it prefer a record that matches the item criteria if there is one.
Try this
this won't work
Select * From MyTable
Where (GroupID = 1 and ItemID = 2) or (GroupID = 1)
this works
IF EXISTS (SELECT 1 FROM MyTable WHERE GroupID = 1 AND ItemID = 2)
BEGIN
SELECT TOP 1 * FROM MyTable WHERE GroupID = 1 AND ItemID = 2
END
ELSE
BEGIN
SELECT TOP 1 * FROM MyTable WHERE GroupID = 1
END
replace MyTable with the name of your table
replace GroupID and ItemID with the name of your columns
replace number 1 and 2 with whichever value associate with columns you wish to filter

Query not resulting correct records

I have this query:
SELECT id, name FROM users
WHERE ((id = 1) OR (5 <= 5))
This should result all records as 5 = 5, but it's not. It's only resulting records where the id = 1.
What am I doing wrong?
EDIT:
This is the full query:
SELECT project_id, project_name, project_description, project_active,
users.user_firstname, users.user_lastname FROM projects
INNER JOIN users ON projects.user_id = users.user_id
WHERE (projects.user_id = 1 || 3 <= 3)
EDIT:
Found it =/
Something was wrong with the join; user_id didn't exist anymore for some reason.
Try:
EXPLAIN EXTENDED SELECT id, name FROM users where id=1 or 5<=5;
SHOW WARNINGS;
u will find mysql executes the query is:
SELECT id,name FROM users where 1
just means (id=1 or 5<=5) is same as 1
So do you want all records smaller or equal 5?
if so its only
SELECT id, name FROM users
WHERE id <= 5
Beside that
(5 <= 5)
is "5" a table row like id?
Do this
$q = ("SELECT id, name FROM users WHERE id = 1 || 5 <= 5");
This is how I do it.