11

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
...
lr100
  • 648
  • 1
  • 9
  • 29
  • `SELECT users.email, users.password, data.data_1, data.data_2 FROM users JOIN data ON users.user_id=data.user_id` – juergen d Jun 20 '14 at 17:05
  • This question has been asked a lot before. heres 1 example just from a quick google search. http://stackoverflow.com/questions/9853586/sql-join-multiple-tables – Chase Jun 20 '14 at 17:06
  • This only grabs a single record based on an given email. I am looking for way to join the 2 tables and get all rows (but only the email, password, data_1, and data_2 columns). – lr100 Jun 20 '14 at 17:07

6 Answers6

31

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.

VMai
  • 10,156
  • 9
  • 25
  • 34
5

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
flo_badea
  • 774
  • 5
  • 8
3

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..

M.Usman
  • 2,049
  • 22
  • 25
2

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/

Caleb Palmquist
  • 448
  • 2
  • 7
  • 15
0
SELECT users.email, users.password, data.data_1, data.data_2 FROM 
users JOIN data ON  users.user_id=data.user_id
WHERE users.email='$user_email';
Tbone
  • 129
  • 1
  • 9
0

you have to use inner join which returns records when there is a common field matches in both tables.. for ex in your case

select * from table1 
  inner join table2 on table1.user_id=table2.user_id

will return all the records in both the table by matching the common fields

vivek
  • 309
  • 1
  • 2
  • 8