Fetch rows in MySQL which are not present in both columns from another table

Harika B

There are two tables with details as mentioned below in MySQL:

TABLE 1:

+-------------+---------------+
 | user_id     |  isactive     |
 +-------------+---------------+
 | aaa         | 0             |
 +-------------+---------------+
 | bbb         | 0             | 
 +-------------+---------------+ 

TABLE 2:

 +-------------+---------------+-----------+
 |store_no     |  owner        | store     |
 +-------------+---------------+-----------+
 |1234         | aaa           | aaa,xyz   |
 +-------------+---------------+-----------+
 |1006         | aaa           | aaa       | 
 +-------------+---------------+-----------+
 |1005         | ccc           | www       | 
 +-------------+---------------+-----------+

I need to fetch rows from table 1 whose entries are neither in 'owner' nor in 'store' column of table 2. For example, in the above scenario, the resultset should contain 'bbb'. I tried using find_in_set, locate etc but could not fetch the details as required. Please help..

Updated Tables format

Query:

select a.user_id from table1 u
left outer join table2 a
on (owner=user_id or concat(',',store,',') like concat('%,',user_id,',%'))
where (find_in_set(user_id,owner) = 0 or find_in_set(user_id,store) = 0)
and  isactive=0

FYI, store column can have concatenated values of more than one userid

1000111

You can try using NOT EXISTS

SELECT 
T1.user_id
FROM TABLE_1 T1
WHERE NOT EXISTS (
    SELECT 1 
      FROM 
    TABLE_2 T2
    WHERE T2.owner = T1.user_id OR FIND_IN_SET(T1.user_id,T2.store) > 0
);

SQL FIDDLE DEMO

Suggestion:

Is storing a delimited list in a database column really that bad?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fetch rows in MySQL which are not present in both columns from another table

From Dev

mysql: select rows from another table as columns

From Dev

Need to fetch records from one table which is not present in another one table

From Dev

Need to fetch records from one table which is not present in another one table

From Dev

delete rows from a table that are not present in another

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

Pandas extract columns from dataframe which are not present in another dataframe

From Dev

Select records from a table where two columns are not present in another table

From Dev

how to get value from one table which is not present in another that?

From Dev

Mysql query to get rows of a table as columns of another, with column names from third table

From Dev

How to get all the rows from MySQL table which have same values for multiple columns?

From Dev

mysql - move rows from one table to another

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

MySQL - Join & Count rows from another table

From Dev

insert multiple rows mysql from another table

From Java

Select rows which are not present in other table

From Dev

MySQL: Update rows in table, from rows with matching key in another table

From Dev

MySQL query to select rows from table 2 if *all* rows from table 1 are not present

From Dev

Display records from one table which is not in another table by multiple columns

From Dev

Get Sum of score of a team which is present in multiple rows and two columns MySql PHP

From Dev

Oracle Temporary table columns and data from another table's rows

From Dev

Table rows into columns in mysql

From Dev

How to select values which are not present in another table?

From Dev

how to fetch multiple rows from mysql to java table in swings

From Dev

how to fetch a number of rows...from table in mysql using php

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

fetch 2 columns from table in 2 array php mysql

From Dev

ORDER a table from an column which is in another table in Mysql

Related Related

  1. 1

    Fetch rows in MySQL which are not present in both columns from another table

  2. 2

    mysql: select rows from another table as columns

  3. 3

    Need to fetch records from one table which is not present in another one table

  4. 4

    Need to fetch records from one table which is not present in another one table

  5. 5

    delete rows from a table that are not present in another

  6. 6

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  7. 7

    Pandas extract columns from dataframe which are not present in another dataframe

  8. 8

    Select records from a table where two columns are not present in another table

  9. 9

    how to get value from one table which is not present in another that?

  10. 10

    Mysql query to get rows of a table as columns of another, with column names from third table

  11. 11

    How to get all the rows from MySQL table which have same values for multiple columns?

  12. 12

    mysql - move rows from one table to another

  13. 13

    MYSQL - UPDATE multiple rows from another table

  14. 14

    MySQL - Join & Count rows from another table

  15. 15

    insert multiple rows mysql from another table

  16. 16

    Select rows which are not present in other table

  17. 17

    MySQL: Update rows in table, from rows with matching key in another table

  18. 18

    MySQL query to select rows from table 2 if *all* rows from table 1 are not present

  19. 19

    Display records from one table which is not in another table by multiple columns

  20. 20

    Get Sum of score of a team which is present in multiple rows and two columns MySql PHP

  21. 21

    Oracle Temporary table columns and data from another table's rows

  22. 22

    Table rows into columns in mysql

  23. 23

    How to select values which are not present in another table?

  24. 24

    how to fetch multiple rows from mysql to java table in swings

  25. 25

    how to fetch a number of rows...from table in mysql using php

  26. 26

    Delete rows in MySQL matching two columns in another table

  27. 27

    Delete rows in MySQL matching two columns in another table

  28. 28

    fetch 2 columns from table in 2 array php mysql

  29. 29

    ORDER a table from an column which is in another table in Mysql

HotTag

Archive