Sahil Mujawar
Sahil Mujawar

Reputation: 85

How to fetch particular row data from two tables?

I have two tables named tbl_1 (columns: id, firstname, lastname) and tbl_2 (columns: id, email, phone).

I want to fetch the data of certain student, for example abc with id = 2). How can I do this in MySQL?
Note: I don't want to fetch all students data.

This one is for all the data:

SELECT 
id, firstname, lastname, email, phone 
FROM tbl_1, tbl_2 
WHERE tbl_1.id = tbl_2.id

Upvotes: 1

Views: 71

Answers (1)

jeprubio
jeprubio

Reputation: 18002

Just add the selected id. Ex:

SELECT id, firstname, lastname, email, phone 
FROM tbl_1, tbl_2 
WHERE tbl_1.id = tbl_2.id 
AND tbl_1.id = 2

Or using join:

SELECT id, firstname, lastname, email, phone 
FROM tbl_1
INNER JOIN tbl_2 
ON tbl_1.id = tbl_2.id
WHERE tbl_1.id = 2;

Upvotes: 2

Related Questions