mysql - SQL从4个表中选择数据

标签 mysql select

我想从 4 个表中选择数据。我有:

transactions: no_transactions, date, quantity, no_tank, id_cards, no_terminal
tanks: no_tank, type , price, temperature
terminals: no_terminal, location
cards: id_card , no_card , id_person
people: id_person , first_name, last_name

我想从交易中选择所有 no_transactions、日期、数量, 储 jar 的类型、价格、温度,其中 tansk.no_tank = transactions.no_tank 终端位置,其中terminals.no_terminal = transactions.no_terminal 名字、姓氏来自 people.id_person = cards.id_person(但必须是 cards.id_card = transactions.id_cards)。

我该怎么做?

我尝试这样做:

SELECT no_transactions, date, quantity FROM transactions
UNION
SELECT type , price, temperature FROM tanks WHERE tansk.no_tank = transactions.no_tank
UNION
SELECT location FROM terminals WHERE terminals.no_terminal = transactions.no_terminal
UNION
SELECT first_name, last_name FROM people WHERE people.id_person=(select id_person where cards.id_card = transactions.id_cards)

但它不起作用:( 有人可以帮忙吗?

最佳答案

尝试使用以下查询

select no_transactions, date, quantity from transactions, type , price,
temperature,location,first_name, last_name from transactions AS TR
INNER JOIN tanks AS TK on (TR.no_tank=TK.no_tank) 
INNER JOIN terminals As TM on (TR.no_terminal=TM.no_terminal) 
INNER JOIN cards CD on (TR.id_card=CD.id_card)
INNER JOIN people P on (CD.id_person=P.id_person)
where your condition if you need.

关于mysql - SQL从4个表中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502031/

相关文章:

laravel - 如何使用 Laravel 在下拉列表中显示数据库中的选定值?

mysql - 获取排序中特定项目的订单 ID

sql - Oracle - 仅从列中选择最大值

mysql - UNION vs GROUP BY 或更好的解决方案

Mysql 按 y 列的字母顺序排列所有具有 null x 列的行,然后按 y 列的字母顺序排列所有没有 null x 列的行

php - MYSQL选择所有具有最近日期的记录

select - ncurses和stdin阻止

mysql - Rails5 : Why is a migration breaking my production environment but not my test environment?

php - 如何从 key : value meta table in mysql? 加入数据

mysql - 如何让MySQL表的主键自增?