X
X
XenK2016-08-11 12:13:15
PHP
XenK, 2016-08-11 12:13:15

Add selection by SQL condition?

There is a request like this:

SELECT DATE_FORMAT(`orders`.`date`, '%d.%m.%Y') as `date_sum`, SUM((`orders_items`.`price`)*(`orders_items`.`count`))  AS `money_all` 
FROM `orders_items`
LEFT JOIN `orders_groups` ON `orders_items`.`order_group_id` = `orders_groups`.`id`
LEFT JOIN `orders` ON `orders_groups`.`order_id` = `orders`.`id`
LEFT JOIN `goods` ON `goods`.`id` = `orders_items`.`good_id`
LEFT JOIN `orders_status` ON `orders_status`.`id` = `orders_items`.`status_id`
WHERE `orders`.`date` BETWEEN '2016-08-01' AND '2016-08-11'
AND `orders_items`.`status` = 1
AND `orders`.`type` = 1
AND `orders_groups`.`type_id` > 0
AND `orders`.`status` = 1
GROUP BY `date_sum` ORDER BY `orders`.`date` DESC

It is necessary, by adding a condition to the query above, that is, changing the already obtained selection and writing a column next to using the condition:
AND `goods`.`type_id` = 103
get one more column so that it turns out like this:
----------------------------------------
| date_sum    | money_all | money_prod |
----------------------------------------
| 2016-11-08  | 13214141  | 4314123    |
| ...         | ...       | ...        |

How it is better to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
S3Ga, 2016-08-12
@S3Ga

SELECT DATE_FORMAT(`orders`.`date`, '%d.%m.%Y') as `date_sum`, SUM((`orders_items`.`price`)*(`orders_items`.`count`))  AS `money_all` , `gd`.money_prod  AS money_prod
FROM `orders_items`
LEFT JOIN `orders_groups` ON `orders_items`.`order_group_id` = `orders_groups`.`id`
LEFT JOIN `orders` ON `orders_groups`.`order_id` = `orders`.`id`
LEFT JOIN `goods` ON `goods`.`id` = `orders_items`.`good_id`
LEFT JOIN `goods` AS `gd` ON  `goods`.`type_id` = 103
LEFT JOIN `orders_status` ON `orders_status`.`id` = `orders_items`.`status_id`
WHERE `orders`.`date` BETWEEN '2016-08-01' AND '2016-08-11'
AND `orders_items`.`status` = 1
AND `orders`.`type` = 1
AND `orders_groups`.`type_id` > 0
AND `orders`.`status` = 1
GROUP BY `date_sum`,`money_prod` ORDER BY `orders`.`date` DESC

Basically something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question