如何找到长距离相关的表之间的关系? MySQL的
作者:互联网
我在从prestashop数据库中查找表ps_product和ps_carrier之间的关系时遇到问题.架构可在http://doc.prestashop.com/display/PS16/Fundamentals+of+PrestaShop+Development获得.
我需要通过在我的商店加入这两个表来进行更新,但我很难找到好的钥匙.如何撰写查询?
解决方法:
表表示业务关系/关联.你提到的“关系[船舶]”是FK(外键),查询不需要它们.它们声明某些列的子行值也必须是某些键列的子行值.所需要的是知道当它在表中时关于当前业务情况的行. (鉴于出现的情况,确定FK和其他约束.)
从Required to join 2 tables with their FKs in a 3rd table开始:
Every base table comes with a predicate–a statement template parameterized by column names. The table value is the rows that make its predicate into a true statement.
A query also has a predicate. Its value also is the rows that make its predicate true. Its predicate is built up according to its FROM, WHERE and other clauses.
(CROSS or INNER) JOIN puts AND between predicates; UNION puts OR between them; EXCEPT inserts AND NOT and ON & WHERE AND in a condition; when SELECT drops a column C from T, it puts FOR SOME (value for) C in front of T’s predicate. (Etc for other operators.)
所以给定
-- rows where product [id_product] is supplied by [id_supplier] ...
ps_product(id_product, id_supplier, ...)
-- rows where carrier [id_carrier] has reference [id_reference] ...
ps_carrier(id_carrier, id_reference, ....)
我们写
ps_product s
JOIN ...
ON s.id_product = ...
...
JOIN ps_carrier c
ON ... = id_carrier
WHERE ...
获取行在哪里
product [p.id_product] is supplied by [p.id_supplier] ...
AND ...
AND s.id_product = ...
...
AND carrier [c.id_carrier] has reference [c.id_reference] ...
AND ... = id_carrier
AND ...
您需要知道表的谓词然后将表ON或WHERE条件连接在一起,以便生成的谓词用于您想要的行.
Is there any rule of thumb to construct SQL query from a human-readable description?
标签:prestashop,relationship,mysql,join,foreign-keys 来源: https://codeday.me/bug/20191003/1850412.html