I have 4 tables with foreign key constraints between them. At present, I only know the username of the first table users. I want to get the quantity field in the third table and the gname field in the 4th table. The database structure is as follows,thank you!
My idea is to divide the query into two steps, first query the gid and quantity fields in the cartitem table, save the data, and then query gname according to gid. It feels a little troublesome.
select
gname
from goods
where goods.gid in (
select gid
from cartitem
where cartid = (
select cartid
from cart
where userid = (
select userid
from users
where name = 'admin'
)
)
);
1条答案
按热度按时间kwvwclae1#
In your case using subquery might be a bit tedious, consider using join
Here is my solution using join