MySQL dos tablas independientes cuya unión no existe en la tercera tabla

I have three tables A (Master), B (Master) and C (Transaction). The combination of A.id and B.id exists in C. I want that data of A where combination of A.id and B.id do not exist in table C.

I tried the following but get no rows selected.

SELECT A.* from A, B, C where A.id != C.id and B.id != C.id

I am not that good at creating queries.

Can someone please form a query for this?

preguntado el 05 de mayo de 13 a las 14:05

Could you add what you tried? That could make people more willing to help. -

Can you add the table definitions? -

I think there is some problem with your db struture -

3 Respuestas

SELECT * 
FROM A 
WHERE A.id NOT IN (
    SELECT Aid FROM C)

Your question is a bit strange though, this query selects all the id's of A which are not in table C.

contestado el 05 de mayo de 13 a las 14:05

So, i do not need to use table B? - kunal khemka

What I assume, is that you have 2 columns in table C, one with the id of A and one with the id of B. So if an Id of A does not occur in table C, it is obviously not connected to an id of B - Rick Hoving

SELECT A.*, B.* 
FROM A , B
WHERE A.id = B.id AND A.id NOT IN (SELECT Aid FROM C) AND B.id NOT IN (SELECT Bid FROM C)

contestado el 05 de mayo de 13 a las 14:05

I don't think you understood the question. Only the data of A is needed. Therefore Table B is not needed in your query - Rick Hoving

SELECT * FROM A,C LEFT JOIN B ON B.id = A.id LEFT JOIN B ON B.id = C.id WHERE A.id=C.id AND B.id IS NULL

Respondido 11 Jul 15, 15:07

Format it properly. Care to add it as a code and add few words? - Himanshu Mishra

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.