selecting values from a reference table

Jordan Davis

I have a users table and have roles such as admin|manager|employee which are represented in reference table called reference.

user table - id|first_name|last_name|type|status

reference table - id|table|type|key|value

Now the reference table contains integer key that match values so user.type has 0-admin,1-manager,2-employee which looks something like this

table:user
type:type
key:0
value:admin

My problem is when I have to values in a table which need to access the reference table.

table:user
type:status
key:0
value:enabled

Question: How can I access two reference table values in one statement?

//STATEMENT

SELECT a.id,a.first_name,a.last_name,b.value as user_type,b.value as user_status
FROM user AS a 
JOIN reference as b 
ON 'user'=b.table AND 'type'=b.type AND a.type = b.value AND a.status = b.value
nb1987

You can join to the reference table twice (or three times, or four times...). Just give it two different aliases:

SELECT a.id,a.first_name,a.last_name,b.value as user_type,b.value AS user_type, b2.value as user_status
FROM user AS a 
JOIN reference AS b 
ON 'user'=b.table AND 'type'=b.type AND a.type = b.value 
JOIN reference AS b2
ON 'user'=b2.table AND 'status'=b2.type AND a.status = b2.value

Unless I'm mis-interpreting your requirements, I believe the above is what you are seeking.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

selecting duplicate values with a condition from a mysql table

From Dev

Selecting specific values from a table using BeautifulSoup

From Dev

Why NULL values are excluded while selecting values from table

From Dev

Selecting from a table the next values less than a table element

From Dev

Jquery selecting values from on table while in another table

From Dev

Selecting frequent values from multiple columns from R table

From Dev

Update table by selecting values from multiple tables sql

From Dev

Oracle - selecting constant values as M x N table from dual

From Dev

Update table by selecting values from multiple tables sql

From Dev

selecting a column values from a Table using LINQ (Entity Framework)

From Dev

Selecting multiple random values from 1 table? PHP

From Dev

SQL - Selecting a column from another table twice with different values

From Dev

Selecting distinct values from multiple column of a table with their count

From Dev

Selecting unique values from self-referencing table

From Dev

Selecting all values from a table in mysql using a stored procedure

From Dev

Selecting values from a combobox

From Dev

Selecting values from List

From Dev

Selecting values from a combobox

From Dev

Selecting everything from table 1 with average AND distinct values from table 2 and table 3

From Dev

Selecting from table into a column

From Dev

Not selecting data from table

From Dev

Selecting information from table

From Dev

Selecting values from a table where values from one column is divided into multiple columns

From Dev

Selecting, Storing, Selecting from different table

From Dev

selecting the largest n values and smallest n values simulataneously from a table mySQL

From Dev

Selecting Matching values from three different table and combining them in one table in Oracle

From Dev

Selecting Values from different arrays

From Dev

Selecting random values from dictionary

From Dev

selecting values from javascript in a for loop

Related Related

  1. 1

    selecting duplicate values with a condition from a mysql table

  2. 2

    Selecting specific values from a table using BeautifulSoup

  3. 3

    Why NULL values are excluded while selecting values from table

  4. 4

    Selecting from a table the next values less than a table element

  5. 5

    Jquery selecting values from on table while in another table

  6. 6

    Selecting frequent values from multiple columns from R table

  7. 7

    Update table by selecting values from multiple tables sql

  8. 8

    Oracle - selecting constant values as M x N table from dual

  9. 9

    Update table by selecting values from multiple tables sql

  10. 10

    selecting a column values from a Table using LINQ (Entity Framework)

  11. 11

    Selecting multiple random values from 1 table? PHP

  12. 12

    SQL - Selecting a column from another table twice with different values

  13. 13

    Selecting distinct values from multiple column of a table with their count

  14. 14

    Selecting unique values from self-referencing table

  15. 15

    Selecting all values from a table in mysql using a stored procedure

  16. 16

    Selecting values from a combobox

  17. 17

    Selecting values from List

  18. 18

    Selecting values from a combobox

  19. 19

    Selecting everything from table 1 with average AND distinct values from table 2 and table 3

  20. 20

    Selecting from table into a column

  21. 21

    Not selecting data from table

  22. 22

    Selecting information from table

  23. 23

    Selecting values from a table where values from one column is divided into multiple columns

  24. 24

    Selecting, Storing, Selecting from different table

  25. 25

    selecting the largest n values and smallest n values simulataneously from a table mySQL

  26. 26

    Selecting Matching values from three different table and combining them in one table in Oracle

  27. 27

    Selecting Values from different arrays

  28. 28

    Selecting random values from dictionary

  29. 29

    selecting values from javascript in a for loop

HotTag

Archive