Using if() in a where clause - mysql

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.

Related

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.

Find the same list ID of four tuples in a stored procedure

I'm writing a procedure...
I have 4 parameters in this procedure.
I want to find the same list ID from a Table using this four parameters.
With other words, if all IDs have same List_ID then return List_ID, if
not return NULL, and admitting that some IDs might be NULL, and some
IDs repeats in that table, so it should not fail if ID is null or
repeats
The difficult part is that not all the times I have that four IDs set , and I might have only two IDs set and other two set as NULL.
For example:
Table A
------------------------
ID List_ID
------------------------
1 10
2 10
3 10
4 10
The only solution I see is something like this:
SET id1 = (Select List_ID From Table_A Where ID = _ID_Param1);
SET id2 = (Select List_ID From Table_A Where ID = _ID_Param2);
SET id3 = (Select List_ID From Table_A Where ID = _ID_Param3);
SET id4 = (Select List_ID From Table_A Where ID = _ID_Param4);
#Then I have to check if all ids are same
IF id1 = id2 = id3 = id4 THEN I found the same List_ID
And sometime _ID_Param is set as NULL, so I might have only 1 or 2 or all 4
Sorry If I'm not explaining this very well... but I don't know how to tell this situation, and my knowledges are limited, I need some help
UPDATE
this is close to what I need:
SELECT
IF( (
MIN( List_ID ) = MAX( List_ID )
AND COUNT( * ) = (Select Count(*) From (SELECT _ID_Param1 AS val
UNION ALL
SELECT _ID_Param2
UNION ALL
SELECT _ID_Param3
UNION ALL
SELECT _ID_Param4) Temp Where Temp.val is not null
) ) , List_ID, NULL
) AS LID
FROM table_a
WHERE ID IN ( _ID_Param1, _ID_Param2, _ID_Param3, _ID_Param4 )
The Only Wrong thing is that it will return the List_ID even if a parameter not exists in table and there is a duplicate of other parameters And it count it twice. How to exclude such case, to check if all params exists
You can do this in one query:
select #all_same := (min(List_ID) = max(List_ID) and count(*) = 4)
from table_a
where id in (_ID_Param1, _ID_Param2, _ID_Param3, _ID_Param4);
This assumes that each id is unique in table_a. That is how your queries are set up.
You can use the COUNT aggregate function to find tuples
Select List_ID, COUNT(*) as tuple
FROM Table_A
WHERE ID = _ID_Param_1 OR ID = _ID_Param_2 ... etc
GROUP BY List_ID
HAVING COUNT(*) = 4;
This will return List_IDs that appears 4 times for a given set of parameters.

select where id == value and id != value mysql

I have a table with values like this
listid email
1 test#email.com
5 test#eamil.com
1 test123#email.com
From the above example you can see that the same email can show up in the email column with a different listid.
I want to return emails that have a listid of 1 but not not also have a listid of 5. In that case the only email returned would be test123#email.com.
For now I have
SELECT `email`,`listid` FROM `table` WHERE `emailaddress` LIKE '%#email%' AND `listid` != 5 AND `listid`=1
Of course this does not work because test#email does have a listid of 1 too. How can I exclude test#email from the results?
You could use the exists operator:
SELECT `email`,`listid`
FROM `table` t
WHERE `emailaddress` LIKE '%#email%' AND
`listid` = 1 AND
NOT EXISTS (SELECT *
FROM `table` t2
WHERE t1.`emailaddress` = t2.`emailaddress` AND
t2.`listid = 5)
You could use a join:
SELECT t.email, t.listid
FROM `table` t
JOIN `table` t2 ON t.email = t2.email AND t2.listid != 5
WHERE t.listid = 1
Here's a way using conditional aggregation that's generally fast. The query selects emails with listid 1 or 5, and uses conditional aggregation to remove emails that have listid 5 and returns emails that only have listid 1.
select email
from mytable t
where email LIKE '%#email%'
and listid in (1,5)
group by email
having sum(listid = 5) = 0
This query can take advantage of an index on listid

MySQL filter by values in a group

I need to execute a MySQL query kind of
select * from table where id = 1 or id = 2 or id = 3 or id = 4 or id = 5 or id = 6
Is there any expression to reduce the sql grouping the values?, something like
select * from table where id oneof (1,2,3,4,5,6)
I have the same question for and and and and ...
Yes you need to use in function.
select * from table where id in (1,2,3,4,5,6)
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

MySQL select distinct and determine if only one user_id exists

I would like to run a query to show if there is one or more than one user_id for given poll_id.
Example 1: poll_id 1106 has only user_id 1 for all it's rows.
Example 2: poll_id 1106 has more than one user_id 1 for all it's rows.
Using the example given below, this php code works:
$sql = "
SELECT 1
FROM xf_poll_vote
WHERE poll_id='1106'
having count(distinct user_id) > 1";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection);
// format results by row
while ($row = mysql_fetch_array($sql_result)) {
$user_id[] = $row["user_id"];
}
$count = count($user_id);
echo $count;
Just a COUNT / DISTINCT I think.
SELECT poll_id, CASE WHEN COUNT(DISTINCT user_id) > 1 THEN 'Many' ELSE 'One' END AS ManyOrOne
FROM SomeTable
GROUP BY poll_id
select 1
from yourTable
where poll_id = #yourPoll_id
having count(distinct user_id) > 1
if returns you anything, that poll_id have more than one user. If not, only have one or zero users