A
A
Alexey Bespalov2014-01-15 17:27:01
MySQL
Alexey Bespalov, 2014-01-15 17:27:01

There are 3 tables (A, B, C). B - connective (many-to-many). How to select all records from A but specific?

There are 3 tables (A, B, C). B - connective (many to many).
Table A
id | title
Table B
A_id | С_id
Table C
id | code
We need to select all records from A, but in a specific way. If a record from table A has code 1 in table C, then it should be in the results, if code = 0, then exclude it.
Example:
A

id | title
1  | title 1
2  | title 2
3  | title 3

B
A_id | С_id
1     | 1
1     | 2
2     | 3
3     | 4

C
id | code
1  | 0
2  | 1
3  | 0
4  | 1

In the results of the sample, there is a record from table A with 3 (title 3).
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Lomadurov, 2014-01-15
@nulldef

SELECT * FROM A
  INNER JOIN B
    ON A.id = B.A_id
  INNER JOIN C
    ON B.B_id = C.id
GROUP BY A.id
  HAVING C.code = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question