Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用于从多个表检索信息的SQL语句_Mysql_Sql_Select_Join - Fatal编程技术网

Mysql 用于从多个表检索信息的SQL语句

Mysql 用于从多个表检索信息的SQL语句,mysql,sql,select,join,Mysql,Sql,Select,Join,我需要一些帮助来构造SQL语句。 我只知道SQL的基本知识(insert、delete、alter、where、select),但从未真正处理过聚合函数或连接 下面是设置: 表A 客户id 产品标识 状态码 表B 客户id 土地标识 表C 国家识别号 表D 国家识别号 国家代码 表E 产品标识 国家代码1 SQL语句应该输出的是:表A中的statusCode为1、2或3且country_code==country_code1的所有行 其中,国家代码可通过表B、C、D获得

我需要一些帮助来构造SQL语句。 我只知道SQL的基本知识(insert、delete、alter、where、select),但从未真正处理过聚合函数或连接

下面是设置:

表A

  • 客户id
  • 产品标识
  • 状态码
表B

  • 客户id
  • 土地标识
表C

  • 国家识别号
表D

  • 国家识别号
  • 国家代码
表E

  • 产品标识
  • 国家代码1
SQL语句应该输出的是:表A中的statusCode为1、2或3且country_code==country_code1的所有行

其中,国家代码可通过表B、C、D获得,国家代码1可通过表E获得

请不要发布关于数据库结构本身的答案,因为我无权更改它们

我的方法是这样的,但很明显,这是非常错误的,因为我是SQL初学者:

SELECT * FROM TableA
INNER JOIN TableB ON TableA.cust_id = TableB.cust_id
INNER JOIN TableC ON TableB.landId = TableC.country_id
INNER JOIN TableE ON TableA.prod_id = TableE.product_id
INNER JOIN TableD ON TableE.country_code1 = TableD.country_code
WHERE statusCode IN (1,2,3)
不经意间(未经测试,我不能100%确定这就是您的要求):


从我的头顶上

使用FK连接两组表 加入这些团体, 限制那个超集 还有更多

SELECT * 
FROM (tableA A INNER JOIN tableB B ON A.cust_id=B.cust_id)

INNER JOIN  tableE E ON E.product_id=A.prod_id
INNER JOIN (tableC C INNER JOIN tabldeD D ON D.country_id)
ON D.country_code = E.country_code1
WHERE A.statusCode IN(1,2,3)

我们不必担心国家代码位,因为它位于“加入”中。

谢谢,但这不会产生交叉产品吗?表A、B、C、D和E都是非常大的表。考虑到这仍然是一个可能的解决方案吗?如果您的表非常大,我无法对性能进行评论。我通常使用这种方法,因为我已经习惯了。我还没有遇到糟糕的表现(如果指数是正确的)。谢谢你,这对我来说太棒了:)
SELECT * 
FROM (tableA A INNER JOIN tableB B ON A.cust_id=B.cust_id)

INNER JOIN  tableE E ON E.product_id=A.prod_id
INNER JOIN (tableC C INNER JOIN tabldeD D ON D.country_id)
ON D.country_code = E.country_code1
WHERE A.statusCode IN(1,2,3)