Mysql select rows comaparing with another table records

Hashmi

Ok, so I have a table that contains optionID, optionName. Its called user_options. I have a second table that contains userID, optionID. Its called user_selected_options.

Options in user_options are stored for a user in user_selected_options, which is fine. Now, when editing a user, I just want to show the options from user_options which are not selected for that user in user_selected_options as select boxes. How can I do this?

I hope this question make sense. Please let me know if it is unclear and I will explain further.

Thanks for any tips.

Gordon Linoff

Here is one method using not exists:

select uo.*
from user_options uo
where not exists (select 1
                  from user_selected_options uso
                  where uso.optionId = uo.option_id and uso.userId = $userId
                 );

For best performance, create an index on user_selected_options(option_id, userId).

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 rows from another table as columns

From Dev

Select rows not in another table

From Dev

MYSQL: Select entries in relation to multiple rows of another table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

MySQL select Distinct records in 1 table and count each group based on values in another table

From Dev

Select Records between Range from another table

From Dev

Select records that are only associated with a record in another table

From Dev

Select from one table if no records found in another

From Dev

How to reorder records / rows in a MySQL table

From Dev

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

From Dev

MySQL - Select multiple rows from one table whose IDs are stored in another table

From Dev

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

From Dev

MYSQL - Select all rows in a table where it's ids in another table as string field

From Dev

MySQL - How to select rows in a table where id value is in a comma delimited field in another table?

From Dev

requiring records to exist if they exist in another table in mysql

From Dev

mysql joins exlude matching records in another table

From Dev

MySQL select records from related table

From Dev

Select rows from a table matching other table records

From Dev

Select rows from a table matching other table records

From Dev

Insert multiple rows from select into another table

From Dev

SAS: Select rows where the ID is in another table

From Dev

Select rows according row type in another table

From Dev

Select rows with show flag but based on another table

From Dev

Select rows conditionally and insert into another table conditionally

From Dev

Select rows in a table where id is equals to another id in another table

From Dev

Select rows in a table where id is equals to another id in another table

From Dev

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

From Dev

Select records in on table based on conditions from another table?

From Dev

Select from a table based on another table Number of records Oracle

Related Related

  1. 1

    mysql: select rows from another table as columns

  2. 2

    Select rows not in another table

  3. 3

    MYSQL: Select entries in relation to multiple rows of another table

  4. 4

    MySQL select row with two matching joined rows from another table

  5. 5

    MySQL select Distinct records in 1 table and count each group based on values in another table

  6. 6

    Select Records between Range from another table

  7. 7

    Select records that are only associated with a record in another table

  8. 8

    Select from one table if no records found in another

  9. 9

    How to reorder records / rows in a MySQL table

  10. 10

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

  11. 11

    MySQL - Select multiple rows from one table whose IDs are stored in another table

  12. 12

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

  13. 13

    MYSQL - Select all rows in a table where it's ids in another table as string field

  14. 14

    MySQL - How to select rows in a table where id value is in a comma delimited field in another table?

  15. 15

    requiring records to exist if they exist in another table in mysql

  16. 16

    mysql joins exlude matching records in another table

  17. 17

    MySQL select records from related table

  18. 18

    Select rows from a table matching other table records

  19. 19

    Select rows from a table matching other table records

  20. 20

    Insert multiple rows from select into another table

  21. 21

    SAS: Select rows where the ID is in another table

  22. 22

    Select rows according row type in another table

  23. 23

    Select rows with show flag but based on another table

  24. 24

    Select rows conditionally and insert into another table conditionally

  25. 25

    Select rows in a table where id is equals to another id in another table

  26. 26

    Select rows in a table where id is equals to another id in another table

  27. 27

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

  28. 28

    Select records in on table based on conditions from another table?

  29. 29

    Select from a table based on another table Number of records Oracle

HotTag

Archive