[英]I have three table books and users and orders, so I want to select all books with info orders by used_id
我有三個表書和用戶和訂單,所以我想 select 所有的書信息訂單都由 used_id ...................... ......
// get all order for user_id
const conn = await Client.connect()
// const sql = 'SELECT * FROM order_books'
const sql = 'SELECT * FROM books INNER JOIN order_books ON users.id = order_books.user_id;'
const result = await conn.query(sql)
conn.release()
return result.rows
圖書
CREATE TABLE books (
id SERIAL PRIMARY KEY,
title VARCHAR(150),
totalpages integer,
price integer,
author VARCHAR(255),
type VARCHAR(100),
summary text
);
用戶
CREATE TABLE users (
id SERIAL PRIMARY KEY,
firstName VARCHAR(150),
lastName VARCHAR(150),
email VARCHAR(150) NOT NULL UNIQUE,
phone VARCHAR(150),
password VARCHAR(255)
);
訂單
CREATE TABLE order_books (
id SERIAL PRIMARY KEY,
quantity integer NOT NULL,
user_id bigint REFERENCES users(id) NOT NULL,
books_id bigint REFERENCES books(id) NOT NULL,
status VARCHAR(20) NOT NULL
);
用這個
const sql = 'SELECT * FROM books
INNER JOIN order_books ON books.id = order_books.books_id
INNER JOIN users ON users.id = order_books.users
order by users.id asc;'
select
b.*, -- i would select only required columns
ob.*,
u.*
from
books b
join order_books ob (ob.books_id = b.id)
join users u on (u.id = ob.user_id)
order by u.id asc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.