MYSQL喜欢/匹配子串

时间:2017-02-21 10:33:40

标签: mysql sql

我有以下两个陈述,并希望只在一个陈述中得到这个。那可能吗?

声明一,获取过去两天的内容:

SELECT  
    c.ID SUBTAG
FROM 
    `content` c, 
    posts p   
WHERE 
    c.id = p.id_content AND
    from_unixtime(p.scheduled) BETWEEN TIMESTAMPADD(DAY,-2,now()) AND  from_unixtime(UNIX_TIMESTAMP(NOW()))
ORDER BY from_unixtime(p.scheduled)

使用c.id我需要从Orders获得总和(数量)。像那样:

 SELECT 
    sum(Qty) Bestellungen, 
    SUBTAG  
 FROM 
    Orders 
 WHERE SubTag Like '%c.id%'
 GROUP BY SubTag

在表格命令中,列子标签并不完全具有c.id.它只是专栏的一部分。这就是我需要的原因!表格订单中的子标签看起来像132_ 子标签 _kjsdf4382。

表格订单如下:

Date_           Price   Qty     SubTag  
2017-02-20      14.98   1       b1a8cc_2_qgsGairwtape
2017-02-20      14.98   1       b1a8cc_3_qgdfgdirwtape

帖子看起来像:

ID      id_content      scheduled           ad_active   posted 
3015    82              1487754540          0            1

内容如下:

ascsubid    ID              
b1a8cc      82

查询结果应如下所示:

SubTag   Orders
b1a8cc      2

3 个答案:

答案 0 :(得分:1)

这样的东西?

SELECT 
    sum(Qty) Bestellungen, 
    SUBTAG  
 FROM 
    Orders 
 WHERE SubTag in 
    (SELECT c.ID 
     FROM `content` c, posts p   
     WHERE c.id = p.id_content AND
     from_unixtime(p.scheduled) BETWEEN TIMESTAMPADD(DAY,-2,now()) AND 
     from_unixtime(UNIX_TIMESTAMP(NOW()))
     and c.id Like '%c.id%')
 GROUP BY SubTag;

答案 1 :(得分:0)

我认为您正在寻找以下声明。

 SELECT 
    sum(Qty) Bestellungen, 
    SUBTAG  
 FROM 
    Orders 
 WHERE SubTag Like '%' + c.id + '%';
 GROUP BY SubTag

答案 2 :(得分:0)

你可以尝试

SELECT 
    SUM(Qty) Bestellungen, 
    SubTag  
 FROM 
    Orders 
 WHERE EXIST
    (SELECT c.ID 
     FROM `content` c, posts p   
     WHERE c.id = p.id_content 
           AND from_unixtime(p.scheduled) BETWEEN TIMESTAMPADD(DAY,-2,now()) AND from_unixtime(UNIX_TIMESTAMP(NOW()))
           AND Orders.SubTag LIKE CONCAT('%', c.ID, '%') )
 GROUP BY SubTag; 

OR

SELECT 
    SUM(Orders.Qty) Bestellungen, 
    Orders.SubTag 
FROM
    (SELECT  
        c.ID SUBTAG
    FROM 
        `content` c, 
        posts p   
    WHERE 
        c.id = p.id_content AND
        from_unixtime(p.scheduled) BETWEEN TIMESTAMPADD(DAY,-2,now()) AND  from_unixtime(UNIX_TIMESTAMP(NOW()))
    ORDER BY from_unixtime(p.scheduled)
    ) Tag INNER JOIN Orders ON Orders.SubTag LIKE CONCAT('%', Tag.SubTag, '%') 
GROUP BY 
    Orders.SubTag