2015-07-01 59 views
-1

我不能写一个SQL查询,我需要连接两个表并获得两者的公共值。加入在哪里,并包含子句

这样的事情,

select control_id from ProjectImage 
where group_id ="20" 
Join 
(select images from coupon where coupon_name is "test" and images contains control_id of projectImage) 

图像是contol_id的用逗号分隔开的列表。

所以最后我只想要那些存在于优惠券的表格图像列中的control ids

ProjectImage table ---- 
     image_id bigint(20) 
    control_id varchar(255) 
    name varchar(255) 
    project_id bigint(20)  
    group_id bigint(20) 

Coupon table: 
    id bigint(20) 
    image varchar(1250) 
    name varchar(255)  
    status int(11)  
    wafer_id bigint(20) 
+0

选择字段从表1上其中加入表2中的结构 https://dev.mysql.com/doc/refman/5.0/en/join.html – Akhil

+6

首先,它是MySql还是Sql Server?其次,连接应该在where子句之前 –

+1

[MySQL连接where子句]的可能重复(http://stackoverflow.com/questions/1219909/mysql-join-with-where-clause) – Akhil

回答

0

,您可以加入表上的一些关键例如主键,也可以使用类似

select control_id from ProjectImage where group_id ="20" and 
control_id contains(select images from coupon where coupon_name is 
"test" and images contains control_id) 
+0

给出错误 - 在包含(期望一个分号) – PSDebugger

+0

包含需要列和文本,但在这里它无法解决第二个查询 – PSDebugger

0

,如果我得到你的问题吧,您的查询可能应该是这样的

SELECT ProjectImage.control_id, coupon.images 
FROM ProjectImage 
JOIN coupon ON ProjectImage.control_id = coupon.control_id 
WHERE ProjectImage.group_id ="20" 
    AND coupon.coupon_name = 'test' 

如果它不能这样工作,请为我们提供ProjectImage和Coupon的表格结构。

编辑 随着提供,则可以遵循this thread和列图像分成单独的行,然后使用随意加入,例如在临时表

+2

没有什么像coupon.control_id。两个表中都没有共同的列。唯一的事情是projectImage表的control_id是优惠券表中图像的一个子集。 – PSDebugger

+0

@PSDebugger如果没有公共列,那么你不能使用连接。 –

+0

唯一常见的事情是control_id在图像中存在(图像是不同id的集合,其中control_id将是其中的一个)。所以我需要比较projectImage表中的control_id是否存在于图像列中,然后返回。 – PSDebugger