SQL - Show records which meet 2 or more of multiple conditions - mysql

I have a MySQL table of many records. I am trying to find a way to show the records which meet more than one condition of a query. For example, if I had this table.
TABLE NAME: DATA
ID contactid flag flag_type
-----------------------------------
1 99 Volunteer 1
2 99 Uploaded 2
3 100 Via Import 3
4 100 Volunteer 1
5 100 Uploaded 2
with conditions such as:
WHERE (ID > 2) OR (flag = 'Uploaded') OR (flag_type = 1) ..etc..
The output would be where IDs 4 & 5 only would be returned.

You can count the number of conditions in MySQL and use this value:
where ((id > 2) +
(flag = 'Uploaded') +
(flag_type = 1)
) > 1
A boolean value of "true" is treated as 1 AND "false" is treated as 0. So, by adding up the values, you get the number of conditions that are met.
Often, you do this in an order by, to get the most matching first:
where id > 2 or flag = 'Uploaded' or flag_type = 1
order by ((id > 2) +
(flag = 'Uploaded') +
(flag_type = 1)
) desc;

http://sqlfiddle.com/#!2/572e1/11
This solution selects each of your successful conditions as 1 which is added to "factors". Then we show results with at least 2 factors:
SELECT * FROM
(select id, contactid, flag, flag_type,
(CASE WHEN id > 2 then 1 else 0 END) +
(CASE WHEN flag = 'Uploaded' then 1 else 0 END) +
(CASE WHEN flag_type = 1 then 1 else 0 END) AS factors
from DATA
) t
WHERE factors > 1

With your limited question, this is the most that can be provided.
WHERE `ID`>2 AND (`flag`='Uploaded' OR `flag_type`=1) ..etc

You are on the right path, but you need to read first about boolean logic. ORs are going to return records that match any of the conditions and ANDs will return records that match all of the conditions.

Related

mysql update multiple records of multiple tables using one statement based on condition

I have a mysql table for keeping record of two person competitions like this:
- gameid
- id1 // id of player 1
- id2 // id of player 2
- score1 // score of player 1
- score2 // score of player 2
- state // state of the games is initially 0, then the score updates are made and in order to prevent further updates the state must be updated to 1
I need to check the records and update another table "users" based on the scores. ex:
if score1 > score2 I need to update 3 things:
1- the state of the game // from 0 to 1
2- in table "users" add 1 point to the column score for the user with userid = id1
2- in table "users" subtract 1 point from the column score for the user with userid = id2
So far I am able to update 1 and 2 but I need all the 3 updates in one command:
UPDATE dbo.games AS GA , dbo.users AS US
SET GA.state = 1, US.score = US.score + 1
WHERE US.id = GA.id1 AND GA.state = 0 and GA.score1 > GA.score2
I can separate the +1 and -1 commands, it will work fine. But when the command is run, both users' scores should be updated. Can anyone help?
This should do it:
update dbo.games as ga, dbo.users as us
set
ga.state = 1,
us1.score = us1.score + case
when
(ga.score1 > ga.score2 and us.id = ga1.id1)
or (ga.score2 > ga.score1 and us.id = ga2.id2)
then 1
else -1
end
where
ga.state = 0
and ga.score1 <> ga.score2
and us.id in (ga.id1, ga.id2)
The logic is to select two rows in the user table, and then do conditional logic to decide whether to add or remove a point.
Note: you did not tell how you want to handle tied competitions - so this query explicitly ignores them.
UPDATE dbo.games AS GA , dbo.users AS US
SET GA.state = 1, (CASE WHEN US.id =GA.id1 THEN US.score = US.score + 1
ELSE WHEN US.id=id2 THEN US.score =US.score-1 END)
WHERE GA.state = 0 and GA.score1 > GA.score2
This query increments score with 1 when US.id=id1 and decrements score with 1 when US.id =id2

mysql select counts by if else condition

i have a table named cq500_all(to record diffrent doctor feedback)
now i want know counts when condition status is
field dr_1_finish and field dr_2_finish value is all fill 1
and
when field dr_1 different dr_2 (like dr_1=1 and dr_2=0,or dr_1=0 and dr_2=1 )
cause i want to know two doctors feedback counts (when different doctor's feedback on jpg)
for example image show CQ500-CT-1_36_08.jpg and CQ500-CT-1_36_09.jpg is match my select counts
it will be two (select counts result)
how to make the query on mysql?
You can count as
select count(*) as total
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1 and dr_1 != dr_2
You will got result in total
Pretty much just the way you've described it:
select *
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1
and dr_1 != dr_2
or (if dr_1 or dr_2 might not be just 0 and 1):
select *
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1
and ((dr_1 = 1 and dr_2 = 0) or (dr_1 = 0 and dr_2 = 1))

MySQL group by based on column having a given set of data

I want to produce an accurate dataset from mysql based on true and false logic, if I have say 4 rows and a column has status, if in the status any row has a zero the data group should be regarded as false else true
e.g
k | s
--------- //This if i group by k, it returns 1 while i
a 0 //want it to return 0 on group
a 0
a 1
a 1
k | s
--------- //This one if i group by k will return 1 which is very true
a 1
a 1
a 1
a 1
Suggestions
Try this query:
SELECT k,
CASE WHEN SUM(s) < COUNT(s) THEN 0 ELSE 1 END AS label
FROM yourTable
GROUP BY k
This would work well assuming that the only values in the s column are 0 or 1. The logic here is that if even a single row has a zero value, then the SUM(s) for a given k group would be less than the number of records in that group.

SQL - Order By "Custom Preference List"

Scenario:
I have a column in a MySql table:
my_column - [INT] (Unsigned)
What I need:
I need a query to select ONE ENTRY with conditions as follows:
Given A=n
SELECT FIRST the one with my_column = n
ELSE (my_column = n null result)
SELECT the one with my_column = 0
ELSE
SELECT the one with my_column = whatever
ELSE
Return 0 entries
What I looked into:
I tried:
... WHEREmy_columnIN (n,0) ORDER BYmy_columnDESC LIMIT 1
Which applies for the first two steps, but not for the third one.
Thanks for reading.
Given your description, just use case:
order by (case when field = n then 1
when field = 0 then 2
else 3
end)
Then, of course, you would add limit 1.

match multiple numbers in sql and get count of all matches in rows

I have 3 rows in sql:
sno ids
1. 5,6,7
2. 6,8
3. 5,7,8
I would like to know how many users have max match of (5,6,7) in them for eg: ,Sno. 2 has 1 and Sno 3 has 2 and so on.Is there a query/way to do that or its not possible?
While I'd highly recommend normalizing your database (i.e. not storing comma delimited strings in a colum), here's one option using case and find_in_set:
select sno,
case when find_in_set(5, ids) then 1 else 0 end +
case when find_in_set(6, ids) then 1 else 0 end +
case when find_in_set(7, ids) then 1 else 0 end cnt
from yourtable
group by sno
SQL Fiddle Demo