G
G
gtAlexandr2012-06-05 18:58:32
MySQL
gtAlexandr, 2012-06-05 18:58:32

Is it possible to create such a SQL query?

Good afternoon!

tableA _

f1 f2 f3 f4 f5
one one one a 3
one one one b one
one one one c eleven
one 2 one e 3
one 2 one d one
one 2 one c fifteen


Query 1
SELECT *, MAX(`f5`) AS `f5` FROM `table` WHERE `f1` IN (1) AND `f2` IN (1, 2) AND `f3` IN (1) AND `f4` IN ('a', 'c') GROUP BY `f1`, `f2`, `f3`

Result 1
f1 f2 f3 f4 f5
one one one c eleven
one 2 one c fifteen


Query 2
SELECT *, MAX(`f5`) AS `f5` FROM `table` WHERE `f1` IN (1) AND `f2` IN (1, 2) AND `f3` IN (1) AND `f4` IN ('a', 'c') GROUP BY `f1`, `f2`, `f3`

Result 2
f1 f2 f3 f4 f5
one one one c eleven


Request 3
?

Result 3
f1 f2 f3 f4 f5
one one one c eleven
one 2 one NULL NULL


Question: how should query 2 be reformulated to get result 3?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
RedQuark, 2012-06-05
@RedQuark

User kuzemchik wrote correctly. But in my opinion the question is worded very strange. Why is query 1 given if it is not involved in the question? What does it mean to reformulate a request? It feels like "the only right decision" is being imposed. If the question is from the teacher, then here is the “rewording”:

select 1 As f1, 1 As f2, 	1 As f3,  	'c' As f4, 	'11' As f5
union
select 1 As f1, 2 As f2, 	1 As f3,  	null As f4, null As f5

and don't ask strange questions.

K
kuzemchik, 2012-06-05
@kuzemchik

left join on itself with a where clause. The first three fields are from the first table, the last two are from the second

G
Gesper, 2012-06-05
@Gesper

And it seems to me, or "Result 2" violates the conditions of "Request 2" in the part "AND `f4` IN ('a', 'f')"?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question