I have three related tables: "A (id, val)", "B (id, val)" and a link table with the value "AB (help, bid, val)"
I am asking B to return the values of A, for example:
SELECT A.*
FROM A INNER JOIN AB ON A.id = AB.aid INNER JOIN B ON AB.bid = B.id
WHERE B.val = 'foo';
Each A has many B, and each B has many A.
And the catch I am falling apart is the need to filter the set so that the query returns rows only when AB.val is maximum for any A / B pair
eg. if i have data:
A
id val
1 something
2 somethingelse
IN
id val
1 foo
2 bar
Ab
aid bid val
1 1 3
1 2 2
2 1 1
2 2 4
I would like to select only the first and last lines of AB, since they are the maximum values for each of A, and then they can query against B.val = 'foo' to return only the first line. I do not know how I can limit only the max val row in table AB.
The best I could get is
SELECT *
FROM A
INNER JOIN
(SELECT aid, bid, MAX(val) AS val FROM AB GROUP BY aid) as AB
ON A.id = AB.aid
INNER JOIN B ON AB.id = B.id
WHERE B.val = 'foo'
. -, , , -, . , , , , max (val). , , , , - undefined.
, , , . .
( , , , A - , B - . AB - WordPhoneme "". , . ( - , )