How can I select data from 3 tables in sql?
I have 3 tables:
create table user (
user_id integer primary key autoincrement,
username string not null,
email string not null,
pw_hash string not null
);
create table product (
product_id integer primary key autoincrement,
productname string not null,
productdescription string not null,
);
create produc开发者_如何学Got_review (
product_id integer,
user_id integer,
review,
);
Now, I want to display all the reviews from user_id=1. A simple query for this would be select * from product_review where user_id = 1
However, I want the data listed as -->
username productname review
John iPad3 Super awesome
John SonyVaio Even more awesome
Try this:
SELECT username, productname, review
FROM user a INNER JOIN product_review pr
ON a.user_id = pr.user_id INNER JOIN product p
ON p.product_id = pr.product_id
WHERE a.user_id = 1
精彩评论