2017-02-21 120 views
0

我有以下兩條語句,只希望在一條語句中得到它。那可能嗎?MYSQL LIKE IN /匹配子字符串

說明一,從過去的2天變得內容:

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我需要從訂單金額(數量)。像這樣:

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

在表格命令中,Column Subtag並不完全具有c.id.它只是列的一部分。這就是爲什麼我需要類似的東西! Table Orders中的子標籤看起來像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 
+0

樣本數據和預期的結果將有助於解釋你想要做什麼。 –

+0

好主意。我更新了這個問題 – swapfile

回答

0

我認爲你正在尋找以下聲明。

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

'只想在一個語句中得到這個問題'我不認爲這是問題的答案,您仍然使用兩個不同的SQL語句。 –

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