delete row from table where column does not exist in another table

Justin

I have this SQL query that deletes a user's preferences from USERPREF table if they have not logged in for 30 days (last login date located in MOMUSER table), however, it does not verify that the user still exists in MOMUSER. How can I change this so that if USERPREF.CUSER does not exist in MOMUSER.CODE that the USERPREF row is also deleted in that situation since they will not have a last login date?

    DELETE USERPREF FROM USERPREF
    INNER JOIN MOMUSER ON MOMUSER.CODE = USERPREF.CUSER
    WHERE MOMUSER.LOG_START < GETDATE()-30
Bohemian

Change to an outer join, reverse the condition (so you match users you want to keep) and move it into the join, then use IS NULL to delete rows without joins:

DELETE USERPREF
FROM USERPREF
LEFT JOIN MOMUSER ON MOMUSER.CODE = USERPREF.CUSER
    AND MOMUSER.LOG_START >= GETDATE()-30
WHERE MOMUSER.LOG_START IS NULL

Recall that an outer join returns all nulls when the join misses. By moving the date condition into the join, you get to exercise it but not require a joined row. The where clause filters out all rows that have the kind of data you want to keep - leaving only those you want to delete.

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 From One Table where Does not exist in another

From Dev

Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

From Dev

Select ONE random row from Table1 where id does not exist in Table2

From Dev

Delete rows from a table "A" where column "1" values in "A" match column "1" values in table "B" but column "2" values in "A" do not exist in "B"

From Dev

How to get value of another row if row does not exist in table?

From Dev

How to delete row from table column javafx

From Dev

Delete from table A where no children exist in table A and no children exist in table B

From Dev

SELECT row(s) from a table where value does not exist within a group of related records

From Dev

Search Row not exist in another table

From Dev

Sql how to select from a table that does not exist in another table

From Dev

Use WHERE clause on a column from another table

From Dev

Delete Row in a Table Based on a Lookup Value from Another Table

From Dev

Delete from one table unless row is referenced in another table

From Dev

Select from table where column in select from another table in laravel

From Dev

Select from table where column in select from another table in laravel

From Dev

Android: Table Row Insert Error. Column does not exist?

From Dev

Select all rows from a table except where row in another table with same id has a particular value in another column

From Dev

Delete rows from dataframe is column value does not exist in another dataframe

From Dev

How to select a single row where multiple rows exist from a table

From Dev

How to select a single row where multiple rows exist from a table

From Dev

Get count if column value does not exist in another table

From Dev

How to copy row from a table to another table if the entry is not exist in the new table in sql

From Dev

Delete row in a table by clicking cell in another table

From Dev

SQL code for updating a column where the WHERE condition is from another table

From Dev

SQL code for updating a column where the WHERE condition is from another table

From Dev

MySQL Conditional insert to one table if a certain row does not exist on another table

From Dev

Delete From Table Where Column "In" range - Simple.Data

From Dev

Delete records from a table whose combination of fields do not exist in another table

From Dev

MYSQL query check if ID exist and copy column row values to another table related column names

Related Related

  1. 1

    Select From One Table where Does not exist in another

  2. 2

    Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

  3. 3

    Select ONE random row from Table1 where id does not exist in Table2

  4. 4

    Delete rows from a table "A" where column "1" values in "A" match column "1" values in table "B" but column "2" values in "A" do not exist in "B"

  5. 5

    How to get value of another row if row does not exist in table?

  6. 6

    How to delete row from table column javafx

  7. 7

    Delete from table A where no children exist in table A and no children exist in table B

  8. 8

    SELECT row(s) from a table where value does not exist within a group of related records

  9. 9

    Search Row not exist in another table

  10. 10

    Sql how to select from a table that does not exist in another table

  11. 11

    Use WHERE clause on a column from another table

  12. 12

    Delete Row in a Table Based on a Lookup Value from Another Table

  13. 13

    Delete from one table unless row is referenced in another table

  14. 14

    Select from table where column in select from another table in laravel

  15. 15

    Select from table where column in select from another table in laravel

  16. 16

    Android: Table Row Insert Error. Column does not exist?

  17. 17

    Select all rows from a table except where row in another table with same id has a particular value in another column

  18. 18

    Delete rows from dataframe is column value does not exist in another dataframe

  19. 19

    How to select a single row where multiple rows exist from a table

  20. 20

    How to select a single row where multiple rows exist from a table

  21. 21

    Get count if column value does not exist in another table

  22. 22

    How to copy row from a table to another table if the entry is not exist in the new table in sql

  23. 23

    Delete row in a table by clicking cell in another table

  24. 24

    SQL code for updating a column where the WHERE condition is from another table

  25. 25

    SQL code for updating a column where the WHERE condition is from another table

  26. 26

    MySQL Conditional insert to one table if a certain row does not exist on another table

  27. 27

    Delete From Table Where Column "In" range - Simple.Data

  28. 28

    Delete records from a table whose combination of fields do not exist in another table

  29. 29

    MYSQL query check if ID exist and copy column row values to another table related column names

HotTag

Archive