SQL: Get column where it contains 1 or another - mysql

This is an "either or" kind of query that I can't figure out the best way to approach
SELECT id, serial_number FROM bid_tag WHERE active = 1 AND house_id = 0 OR house_id = 14
So I need to get a house_id that is either 0 or a specific number of "14"

Parentheses are your friend. If you don't use them, logical expressions will be evaluated in the wrong order (in your case, not active items could be listed as well):
active = 1 AND (house_id = 0 OR house_id = 14)
You can also use IN(), more handy if you have several options:
active = 1 AND house_id IN (0, 14)

Related

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 WHERE id = 0 it should show total result

SQLFiddle
In this fiddle example i m trying to use WHERE o.d_id = 1 or 2,3,4 etc but if i use d_id= 0 it should show the total result or we can say the where clause should not work if d_id = 0.
You can do a conditional style WHERE clause like this....
WHERE (?inputvalue = 0 OR ?inputvalue = d_id)
In this case the ?inputvalue is whatever search term you want. You'll provide it from your programming language.
edit Notice that in many queries you can specify an input value just once. For example, if all you want is WHERE d_id = 17 that's easy to specify. You just put the 17 in and you're done.
But, if you follow my suggestion you need to repeat the input value. You need WHERE (0 = 17 OR d_id = 17). That will, when you give it 17, filter your result set to find the seventeens.
And, if you give it 0, like this WHERE ( 0 = 0 OR d_id = 0 ) it will find all results, not filtering on just the zeroes. That's good because there aren't any zeros.
Probably, that's not how WHERE works. If you write a statement like WHERE id = 0, you ask your database to return just the rows where the id column is set to 0. If you want your database to return all values, you have to skip this condition completly

SQL: How to check "if this record exists then that record must also exist" for given ID set

my database table (DWInfo) looks like this:
InstanceID | AttributeID
1 | 1
1 | 2
1 | 3
2 | 1
2 | 4
3 | 1
3 | 2
There are several instances and every instance has multiple attributes.
What I want to achieve is this: for a given set/rule of id's I want to get all InstanceID's which violate the condition, for example let the given ID's be 1 and 2, which means if there is an instance with AttributeID=1, Attribute=2 should also exist for it. In this case the result would be instance two, because this instance violates the condition.
I tried it with JOINS but this only seemed effective for 2 attributes and not more.
Select * from DWInfo dw1 INNER JOIN DWInfo dw2 ON dw1.InstanceID = dw2.InstanceID where dw1.AttributeID != dw2.AttributeID and dw1.AttributeID = 1 AND dw2.AttributeID != 2
Is it possible to solve this problem with a SQL query?
Assuming that each InstanceId can have only one of each different AttributeId, i.e. a unique composite index (InstanceId, AttributeId):
SELECT InstanceID
FROM DWInfo
WHERE AttributeID IN (1,2)
GROUP BY InstanceID
HAVING SUM(AttributeId = 1) = 1
AND COUNT(*) < 2 /* Or SUM(AttributeId = 2) = 0 */
SQLFiddle DEMO
Note that if having AttributeId of 2 means that the instance requires an AttributeId of 1 also.. slightly different logic, this is neater:
SELECT InstanceID
FROM DWInfo
WHERE AttributeID IN (1,2)
GROUP BY InstanceID
HAVING COUNT(*) < 2
Where there exists Attribute 1 find the ones that don't have Attribute 2.
select InstanceID
from DWInfo
group by InstanceID
having
count(case when AttributeID = 1 then 1 end) > 0
and count(case when AttributeID = 2 then 1 end) = 0
This answer is basically the same as Arth's. You might find it beneficial to filter the Attributes in the where clause but it's not strictly necessary. I prefer the standard syntax using case expressions even though the shorthand would be handy if it were portable. I also prefer count over sum in these scenarios.
It's not clear whether you can have duplicates (probably not) and whether Attribute 2 can appear alone. You might have to tweak the numbers a bit but you should be able to follow the pattern.
I think this does what you want:
select instanceid
from dwinfo
where attributeid in (1, 2)
group by instanceid
having count(*) = 2;
This guarantees that you have two matching rows for each instance. If you can have duplicates, then use:
having count(distinct attributeid) = 2
EDIT:
For the conditional version (if 1 --> 2):
having max(attributeid = 2) > 0
That is, if it has 1 or 2, then it has to have 2, and everything is ok.

SQL where particular column values appears

I wasn't sure how to really search for this..
Lets say I have a simple table like this
ID Type
1 0
1 1
2 1
3 0
4 0
4 1
How could I select all ID's which have a type of both 0 and 1?
SELECT id,type
FROM t
GROUP BY id
HAVING SUM(type=0)>0
AND SUM(type=1)>0
You just group by id ,than with HAVING you use post aggregation filtering to check for 0 and 1.
Having is pretty expensive and that query can't hit keys.
SELECT ID FROM foo AS foo0 JOIN foo AS foo1 USING (ID) WHERE foo0.Type=0 AND foo1.Type=1 GROUP BY foo0.id.
A more generalized way of doing this would by to use a CASE column for each value you need to test combined with a GROUP BY on the id column. This means that if you have n conditions to test for, you would have a column indicating if each condition is met for a given id. Then the HAVING condition becomes trivial and you can use it like any multi-column filter, or use the grouping as your subquery and the code looks simpler and the logic becomes even easier to follow.
SELECT id, Type0,Type1
FROM (
SELECT id,
Type0 = max(CASE WHEN type = 0 THEN TRUE END)
, Type1 = max(CASE WHEN type = 1 THEN TRUE END)
FROM t
GROUP BY id
) pivot
WHERE Type0 = TRUE and Type1 = TRUE

MySQL 2 Columns from 2 tables

There we go actually I think it should be ok but it isn't
SELECT animize_users.username, animize_profile.avatar
FROM animize_users, animize_profile
WHERE `animize_profile.userid` = 1 AND `animize_users.id` = 1
LIMIT 0 , 1
Please don't tell me to put this in a a seperate query each - as i don't want to make needless calls...
Just drop the quotes entirely if they're not required, i.e.
SELECT animize_users.username, animize_profile.avatar FROM animize_users au join animize_profile ap on ap.userid = au.id WHERE au.id = 1
If you've got a proper PK on user id, the user shouldn't repeat and really limit 1 would only effect your selection of profile. I'd suggest adding to the where to ensure you get the most appropriate profile.
Also, if "animize" is your product, don't prefix your tables with it, it's redundant.
Try it this way with backticks correctly used:
SELECT animize_users.username, animize_profile.avatar
FROM animize_users, animize_profile
WHERE `animize_profile`.`userid` = 1 AND `animize_users`.`id` = 1
LIMIT 0 , 1