SELECT multiple values to the same key in multiple tables

Motaz M. El Shazly

I have two tables in the following structure

table_1

uid | name | age
1   | John | 24
2   | Adam | 35
3   | Sara | 26

table_2

id | uid | meta_key | meta_value
1  | 2   | location | NY
2  | 2   | school   | NYU
3  | 3   | Location | NY
4  | 3   | school   | XYZ
6  | 1   | location | NY
6  | 1   | school   | NYU

What I am trying to do is to select the users from table_1 where their location is NY and school is NYU

here is the query I tried using with no luck

SELECT
  tabl_1.uid
FROM `tabl_1`, `tabl_2`
WHERE
  tabl_1.uid = tabl_2.uid
  AND table_2.meta_key IN ('location', 'school')
  AND table_2.meta_value IN ('NY', 'NYU')
ORDER BY tabl_1.uid ASC

I have looked everywhere without any luck, if you have a query that works or a link to a solution that would much appreciated, thank you.

Marco

You should try

SELECT t1.uid
FROM tabl_1 t1 INNER JOIN tabl_2 t2
ON t1.uid = t2.uid AND t2.meta_key = 'location' AND t2.meta_value = 'NY'
INNER JOIN tabl_2 t3
ON t1.uid = t3.uid AND t3.meta_key = 'school' AND t3.meta_value = 'NYU'

Check result on http://sqlfiddle.com/#!2/f35ef/1/0

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 multiple values with same ID

From Dev

select from multiple tables with the same column

From Dev

Delete the same set of values from multiple tables

From Dev

Insert same variables and values into multiple tables

From Dev

Select all values from multiple tables

From Dev

Using Join, select values from multiple SQL tables without a foreign key

From Dev

Using Join, select values from multiple SQL tables without a foreign key

From Dev

Storing multiple values with same key in HashMap

From Dev

how to push multiple values to same hash key

From Dev

How to add multiple values to the same key in Map

From Dev

How to add multiple values to the same key in Map

From Dev

Remove dictionary key when the multiple values are the same

From Dev

how to push multiple values to same hash key

From Dev

Select multiple values from the same table

From Dev

select query for multiple columns with same type of values

From Dev

Multiple tables with same model

From Dev

Sequelize joining tables with multiple records with same foreign key

From Dev

SELECT in multiple tables

From Dev

SQL select multiple tables

From Dev

SELECT from multiple tables using composite PRIMARY KEY

From Dev

SELECT query from one table linked with foreign key to multiple tables

From Dev

Select same fields from multiple tables using postgresql

From Dev

Select/Join multiple fields from different tables with same column name

From Dev

Select/Join multiple fields from different tables with same column name

From Dev

how to enter values in multiple tables using same id

From Dev

MySQL select values from Multiple Tables dependent on latest value in one

From Dev

Select values from multiple tables where certain value is zero

From Dev

SQL Select statement - multiple tables allow null values

From Dev

how to select multiple values in a drop down Data tables

Related Related

  1. 1

    Select multiple values with same ID

  2. 2

    select from multiple tables with the same column

  3. 3

    Delete the same set of values from multiple tables

  4. 4

    Insert same variables and values into multiple tables

  5. 5

    Select all values from multiple tables

  6. 6

    Using Join, select values from multiple SQL tables without a foreign key

  7. 7

    Using Join, select values from multiple SQL tables without a foreign key

  8. 8

    Storing multiple values with same key in HashMap

  9. 9

    how to push multiple values to same hash key

  10. 10

    How to add multiple values to the same key in Map

  11. 11

    How to add multiple values to the same key in Map

  12. 12

    Remove dictionary key when the multiple values are the same

  13. 13

    how to push multiple values to same hash key

  14. 14

    Select multiple values from the same table

  15. 15

    select query for multiple columns with same type of values

  16. 16

    Multiple tables with same model

  17. 17

    Sequelize joining tables with multiple records with same foreign key

  18. 18

    SELECT in multiple tables

  19. 19

    SQL select multiple tables

  20. 20

    SELECT from multiple tables using composite PRIMARY KEY

  21. 21

    SELECT query from one table linked with foreign key to multiple tables

  22. 22

    Select same fields from multiple tables using postgresql

  23. 23

    Select/Join multiple fields from different tables with same column name

  24. 24

    Select/Join multiple fields from different tables with same column name

  25. 25

    how to enter values in multiple tables using same id

  26. 26

    MySQL select values from Multiple Tables dependent on latest value in one

  27. 27

    Select values from multiple tables where certain value is zero

  28. 28

    SQL Select statement - multiple tables allow null values

  29. 29

    how to select multiple values in a drop down Data tables

HotTag

Archive