Select rows with show flag but based on another table

John Mark

Can someone help me about this problem.

I have 2 tables:

TASKS:(id,once)
SAVED_tasks (id, task_id) 
  • table where i save tasks..

I need to show all tasks but NOT if the task have value is 1 and if is already inserted in SAVED_tasks table..

EXAMPLE:

tasks:

id, name, once
1, task1, 0
2, task2, 1
3, task3, 0
4, task4, 1

saved_tasks:

id, task_id
1, 1
2, 2
3, 3
4, 4

I need result:

1, task1
3, task3
Saharsh Shah

Try this:

SELECT t.id, t.once
FROM TASKS t 
LEFT JOIN SAVED_tasks st ON t.id = st.task_id 
WHERE (t.once != 1 OR (t.once = 1 AND st.id IS NULL));

OR

SELECT t.id, t.once
FROM TASKS t 
LEFT JOIN SAVED_tasks st ON t.id = st.task_id 
WHERE NOT(t.once = 1 AND st.id IS NULL);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select rows from table based on data from another table

From Dev

select rows from table based on data from another table

From Dev

SQL Hide/Show rows based on row count from another table

From Dev

Select rows into columns and show a flag in the column

From Dev

Select rows not in another table

From Dev

Laravel select random rows from table based on another field

From Dev

PostgreSQL - insert rows based on select from another table, and update an FK in that table with the newly inserted rows

From Dev

Select rows from one table and adjust the values based on rows in another table

From Dev

SQL select flag based on count and/or flag of joined table

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

Oracle select random rows from table based on criteria in another table (Stratified Sampling)

From Dev

MySql select all rows in one table based on MAX value in another table

From Dev

Select data based on another table

From Dev

Expanding query table rows based on another table

From Dev

MySQL Show rows that exist in one table but not it another

From Dev

MySQL Show rows that exist in one table but not it another

From Dev

Show/Hide Table Rows based on Checkbox

From Dev

How to select a quantity of rows from one table based on quantities from another

From Dev

Select rows of a dataframe based on another dataframe in Python

From Dev

Select rows based on occurrences in another column R

From Dev

How to select rows based on the output of another query?

From Dev

Adding rows to a table based on number in another row

From Dev

MySQL - Query For Rows With Updates Based On Another Table

From Dev

Insert multiple rows from select into another table

From Dev

SAS: Select rows where the ID is in another table

From Dev

mysql: select rows from another table as columns

From Dev

Select rows according row type in another table

From Dev

Mysql select rows comaparing with another table records

From Dev

Select rows conditionally and insert into another table conditionally

Related Related

  1. 1

    select rows from table based on data from another table

  2. 2

    select rows from table based on data from another table

  3. 3

    SQL Hide/Show rows based on row count from another table

  4. 4

    Select rows into columns and show a flag in the column

  5. 5

    Select rows not in another table

  6. 6

    Laravel select random rows from table based on another field

  7. 7

    PostgreSQL - insert rows based on select from another table, and update an FK in that table with the newly inserted rows

  8. 8

    Select rows from one table and adjust the values based on rows in another table

  9. 9

    SQL select flag based on count and/or flag of joined table

  10. 10

    MySQL project design - conditionally select from one table based on rows from another select query

  11. 11

    Oracle select random rows from table based on criteria in another table (Stratified Sampling)

  12. 12

    MySql select all rows in one table based on MAX value in another table

  13. 13

    Select data based on another table

  14. 14

    Expanding query table rows based on another table

  15. 15

    MySQL Show rows that exist in one table but not it another

  16. 16

    MySQL Show rows that exist in one table but not it another

  17. 17

    Show/Hide Table Rows based on Checkbox

  18. 18

    How to select a quantity of rows from one table based on quantities from another

  19. 19

    Select rows of a dataframe based on another dataframe in Python

  20. 20

    Select rows based on occurrences in another column R

  21. 21

    How to select rows based on the output of another query?

  22. 22

    Adding rows to a table based on number in another row

  23. 23

    MySQL - Query For Rows With Updates Based On Another Table

  24. 24

    Insert multiple rows from select into another table

  25. 25

    SAS: Select rows where the ID is in another table

  26. 26

    mysql: select rows from another table as columns

  27. 27

    Select rows according row type in another table

  28. 28

    Mysql select rows comaparing with another table records

  29. 29

    Select rows conditionally and insert into another table conditionally

HotTag

Archive