I have a table T1
with columns C1, C2, C3, C4
and T2
with columns C1
and D1
. I'd like to select all rows in T1
where appropriate row in T2
has D1
greater than C2
, C3
and C4
.
I tried something like this
SELECT C1,C2,C3,C4 FROM T1
WHERE (SELECT D1 FROM T2 WHERE C1=T1.C1)>(C2,C3,C4)
and this
SELECT C1,C2,C3,C4 FROM T1
WHERE (SELECT D1 FROM T2 WHERE C1=T1.C1)>MAX(C2,C3,C4)
but what I get is an error in syntax. I could write WHERE clause like this:
WHERE (SELECT D1 FROM T2 WHERE C1=T1.C1)> C2
AND (SELECT D1 FROM T2 WHERE C1=T1.C1)> C3
AND (SELECT D1 FROM T2 WHERE C1=T1.C1)> C4
However, running the same subquery 3 times is unnecessary for it always returns the same result.
Could anyone help? :)
You want the GREATEST()
function, which allows multiple arguments. MAX() accepts only a single argument, and is intended for aggregate operations
SELECT *
FROM T1
...
WHERE T2.D1 > GREATEST(C1,C2,C3,C4)
If it helps, MAX() works "vertically", on a single field in a result set. GREATEST() works "horizontally", and operates within a single record.
Also if I understand correctly, your key is C1... you need to join your tables properly..
SELECT T1.*
FROM T1
LEFT JOIN T2 USING (C1)
WHERE D1 > GREATEST(C2,C3,C4)
I put together a little SQL Fiddle for you here and tried to replicate my understanding of what you are asking.
Here is the query I used:
SELECT one.* FROM one, two
WHERE (two.frags > one.size)
AND (two.frags > one.weight)
AND (two.frags > one.height)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.