简体   繁体   中英

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".

However when I go to join this table with another table propA_Photos, MySQL throws and error. I only want the propA_Photos.photo column joined with the above in one query.

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.

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'
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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