MySql select joined rows that don't exists in third table

Yspd Ieper

I'm having difficulties with creating a sql query that selects a joining of 2 tables where the relationship doesn't exists in a third table.

To be more specific I try to explain it with an example. I have a table member, form and member_form. When a member inserts an answer in the form, it's saved in the member_form table. The forms can be active, and have a deadline. So what I want is to return a result for each member and the form he still didn't fill in. But the form must be active, and deadline must be for example 2018-03-15. I don't know the amount of members or forms. So I can't say from select * where member id is = x. This is needed to send a reminder mail to all the users that didn't fill in the form yet for a specific date.

member
id  |   name    |   email
--------------------------
1   |   Test    |   [email protected]
2   |   test2   |   [email protected]
4   |   test4   |   [email protected]
5   |   test5   |   [email protected]
6   |   test6   |   [email protected]
7   |   test7   |   [email protected]


form
id  |   insert_date         |   deadline_date       |   active
---------------------------------------------------------------
1   |   2018-03-15 00:00:00 |   2018-03-15 00:00:00 |   1   
2   |   2018-02-10 00:00:00 |   2018-05-15 00:00:00 |   0   
3   |   2018-03-15 00:00:00 |   2018-03-15 00:00:00 |   1   
5   |   2018-03-15 00:00:00 |   2018-06-15 00:00:00 |   1   
6   |   2018-03-15 00:00:00 |   2018-05-15 00:00:00 |   1   
7   |   2018-03-15 00:00:00 |   2018-04-15 00:00:00 |   0       


member_form
member_id   |   form_id     |   answer
--------------------------------------
1           |   6           |   1
1           |   2           |   2
1           |   5           |   1
2           |   2           |   1
2           |   3           |   1
4           |   6           |   2
5           |   6           |   3
5           |   7           |   2
6           |   1           |   2
7           |   2           |   1


Result
member_id   |   name    |   email           |   form_id |   insert_date         |   deadline_date       |   active
-------------------------------------------------------------------------------------------------------------------
2           |   test2   |   [email protected] |   6       |   2018-03-15 00:00:00 |   2018-05-15 00:00:00 |   1
6           |   test6   |   [email protected] |   6       |   2018-03-15 00:00:00 |   2018-05-15 00:00:00 |   1
7           |   test7   |   [email protected] |   6       |   2018-03-15 00:00:00 |   2018-05-15 00:00:00 |   1
Max Gaurav
SELECT * FROM form AS f , member AS m WHERE f.deadline_date = '2018-05-15 00:00:00' AND f.active = 1 and !exists(select * FROM member_form AS amf WHERE amf.member_id = am.id And amf.form_id = af.id);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL select row with two matching joined rows from another table

From Dev

How to return all rows, even if they don't have rows in a joined table

From Dev

Select from joined table only if record exists

From Dev

select record from joined table if it exists

From Dev

Use LISTAGG to select multiple rows on joined table

From Dev

Join table in mySQL return me NULL if joined table don't contain row

From Dev

MySQL Select If Table Exists

From Dev

SQL: select similar and rows that don't exist in second table

From Dev

Laravel mysql select count of joined table

From Dev

MySQL select SUM when a table is joined

From Dev

Mysql : select rows from table 1 and test if rows exists in table 2

From Dev

How can I move rows from one table to another where they don't exist in a third?

From Dev

Join sql tables to select records which does not exists in joined table

From Dev

MySQL SELECT query that counts left joined rows takes too long

From Dev

SQL Select and find rows that not exists in other table

From Dev

SELECT rows from a table that don't have related entries in a second table

From Dev

Select rows that don't exist in other table join by data in the second table [SQL]

From Dev

Select rows that don't exist in other table join by data in the second table [SQL]

From Dev

T-SQL convert inner joined table's rows as columns

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

Filtering MySQL Select based on joined table's fields

From Dev

SQL select - include rows that don't exist?

From Dev

Select Rows which don't exist in second table based on multiple columns

From Dev

MySQL averaging joined table

From Dev

MySQL count on joined table

From Dev

How to get rows presented as null value which do not exists in joined table (oracle)?

From Dev

Don't resort data.table rows

From Dev

jQuery Datatable: don't remove table rows

Related Related

  1. 1

    MySQL select row with two matching joined rows from another table

  2. 2

    How to return all rows, even if they don't have rows in a joined table

  3. 3

    Select from joined table only if record exists

  4. 4

    select record from joined table if it exists

  5. 5

    Use LISTAGG to select multiple rows on joined table

  6. 6

    Join table in mySQL return me NULL if joined table don't contain row

  7. 7

    MySQL Select If Table Exists

  8. 8

    SQL: select similar and rows that don't exist in second table

  9. 9

    Laravel mysql select count of joined table

  10. 10

    MySQL select SUM when a table is joined

  11. 11

    Mysql : select rows from table 1 and test if rows exists in table 2

  12. 12

    How can I move rows from one table to another where they don't exist in a third?

  13. 13

    Join sql tables to select records which does not exists in joined table

  14. 14

    MySQL SELECT query that counts left joined rows takes too long

  15. 15

    SQL Select and find rows that not exists in other table

  16. 16

    SELECT rows from a table that don't have related entries in a second table

  17. 17

    Select rows that don't exist in other table join by data in the second table [SQL]

  18. 18

    Select rows that don't exist in other table join by data in the second table [SQL]

  19. 19

    T-SQL convert inner joined table's rows as columns

  20. 20

    MySQL: Select records where joined table matches ALL values

  21. 21

    MySQL: Select records where joined table matches ALL values

  22. 22

    Filtering MySQL Select based on joined table's fields

  23. 23

    SQL select - include rows that don't exist?

  24. 24

    Select Rows which don't exist in second table based on multiple columns

  25. 25

    MySQL averaging joined table

  26. 26

    MySQL count on joined table

  27. 27

    How to get rows presented as null value which do not exists in joined table (oracle)?

  28. 28

    Don't resort data.table rows

  29. 29

    jQuery Datatable: don't remove table rows

HotTag

Archive