Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all data from 2 tables using foreign key

Tags:

sql

mysql

This is the result of separating a single table in two:

Table users:
    user_id (pk, ai)
    email
    password
    last_login

Table data:
    user_id (fk to users.user_id)
    data_1
    data_2

To select a single record when there was only one table:

SELECT users.email, users.password, data.data_1, data.data_2
FROM users,data 
WHERE users.email='$user_email' AND users.user_id=data.user_id";

How do I get all records from both tables having the rows connected by users.user_id=data.user_id?

Row1: email, password, data_1, data2
Row2: email, password, data_1, data2
Row3: email, password, data_1, data2
Row4: email, password, data_1, data2
...
like image 452
lr100 Avatar asked Jun 20 '14 17:06

lr100


People also ask

Can foreign key refer to two tables?

The FOREIGN KEY constraint is a key used to link two tables together.

Can you relate data to multiple tables using a foreign key?

A Foreign Key is a database key that is used to link two tables together. The FOREIGN KEY constraint identifies the relationships between the database tables by referencing a column, or set of columns, in the Child table that contains the foreign key, to the PRIMARY KEY column or set of columns, in the Parent table.

How can I extract data from two tables?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.

How get all values from two tables in SQL?

LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table. FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table.


4 Answers

Using explicit join syntax could help you. Rewrite your query to:

SELECT 
    users.email, users.password, data.data_1, data.data_2
FROM 
    users
INNER JOIN 
    data 
ON
    users.user_id=data.user_id
WHERE 
    users.email='$user_email'

and get all rows without a WHERE condition:

SELECT 
    users.email, users.password, data.data_1, data.data_2
FROM 
    users
INNER JOIN 
    data 
ON
    users.user_id=data.user_id

It separates the concerns: conditions that join tables from conditions that restricts the result set.

like image 124
VMai Avatar answered Oct 10 '22 10:10

VMai


To join the userData table to Users try this:

SELECT u.user_id, u.email, u.password, u.last_login
FROM users u
JOIN userData ud ON (u.userID = ud.userID)

This will return all data where the User ID in the Users table matches the User ID in the userData table.

Edit In addition, there are different kinds of joins:

  • INNER
  • OUTER
  • LEFT
  • RIGHT

For more information on this and their differences check out this handy reference: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

like image 38
Caleb Palmquist Avatar answered Sep 22 '22 08:09

Caleb Palmquist


have you tried this?

SELECT users.email, users.password, data.data1, data.data2
FROM users,data 
WHERE users.user_id=data.user_id

or this?

SELECT users.email, users.password, data.data1, data.data2
FROM users inner join data on users.user_id=data.user_id
like image 5
flo_badea Avatar answered Oct 10 '22 09:10

flo_badea


We can do it as follow...

SELECT users.email, users.password, data.data_1, data.data_2
FROM users,data 
WHERE users.user_id=data.user_id AND users.email='$user_email'

just copy and past above query you may get expected result..

like image 3
M.Usman Avatar answered Oct 10 '22 11:10

M.Usman