0

i have the following two statements and want to get this in only one statement. Is that possible?

Statement One, getting Content from the last 2 Days:

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)

With c.id i need to get sum(qty) from Orders. Like that:

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

Within the table orders the Column Subtag doesnt exactly has the c.id. Its just part of the Column. That's why i need the like! Subtag in Table Orders looks like 132_subtag_kjsdf4382.

Table Orders looks like:

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

posts look like:

ID      id_content      scheduled           ad_active   posted 
3015    82              1487754540          0            1

And content looks like:

ascsubid    ID              
b1a8cc      82

Result from the query should look like:

SubTag   Orders
b1a8cc      2
2
  • Sample data and desired results would help explain what you want to do. Commented Feb 21, 2017 at 11:34
  • good idea. i've updated the question Commented Feb 21, 2017 at 11:46

3 Answers 3

1

Something like this ?

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;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for below statement.

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

1 Comment

want to get this in only one statement I don't think this is an answer to the question, you are still using two different SQL statements.
0

You could try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.