简体   繁体   English

如何联接SQL表

[英]How to JOIN SQL Tables

I cant seem to figure out how to join these two table to execute and return all I need in one payload through a single query. 我似乎无法弄清楚如何将这两个表连接起来,以通过单个查询在一个有效负载中执行并返回所有我需要的东西。 Any Help would be very appreciated. 任何帮助将不胜感激。

As you will see everything work with the first half of the query if I place a ; 如您所见,如果我放置;,则所有内容都适用于查询的前半部分。 at the end of the closing parenthesis following the "IN". 在“ IN”后面的右括号结尾处。

However when I go to join this table with another table propA_Photos, MySQL throws and error. 但是,当我将这个表与另一个表propA_Photos联接时,MySQL会抛出错误。 I only want the propA_Photos.photo column joined with the above in one query. 我只希望在一个查询中将propA_Photos.photo列与上述内容一起加入。

What am I doing wrong? 我究竟做错了什么?

SELECT propA.list_id, propA.list_key, propA.list_value
FROM propA
where list_id = '20141118214124325535000000'
    AND list_key IN ('LIST_1', 'LIST_22', 'LIST_33', 'LIST_31', 'LIST_34', 'LIST_35', 'LIST_36', 'LIST_37', 'LIST_39', 'LIST_40', 'LIST_43', 'LIST_41', 'LIST_46', 'LIST_47') 
INNER JOIN propA_Photos.photo;

You're missing a join clause, I'm not sure if your database is okay with that. 您缺少联接子句,我不确定您的数据库是否可以。

SELECT 
    propA.list_id, propA.list_key, propA.list_value
FROM propA
INNER JOIN propA_Photos.photo
        ON propA_Photos.<<Attr1>> = propA.<<Some_Attribute>>
WHERE list_id = '20141118214124325535000000' 
  AND list_key IN (.. lot of stuff... ) 

When joining tables you need to adhere to a specific syntax and order of operators. 连接表时,您需要遵循特定的语法和运算符顺序。

The where clause should follow the join clauses and you need to specify the condition that determines how the tables should be joined using an on clause. where子句应在join子句之后,并且您需要指定条件,该条件确定如何使用on子句连接表。

You probably want something like this: 您可能想要这样的东西:

SELECT propA.list_id, propA.list_key, propA.list_value 
FROM propA 
INNER JOIN propA_Photos.photo ON propA.some_column = propA_Photos.some_column
-- the join should likely by using primary key = foreign key
WHERE list_id = '20141118214124325535000000' 
  AND list_key IN (
    'LIST_1',  'LIST_22', 'LIST_33', 'LIST_31', 
    'LIST_34', 'LIST_35', 'LIST_36', 'LIST_37', 
    'LIST_39', 'LIST_40', 'LIST_43', 'LIST_41', 
    'LIST_46', 'LIST_47'
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM