【问题标题】:group by is not working with union in MySQLgroup by 不与 MySQL 中的 union 一起使用
【发布时间】:2019-01-19 07:23:04
【问题描述】:

我的查询遇到了一个问题,即 group by 没有与工会合作。 以下是查询:

(select products.*, sum(100000) as count1, product_sku.price AS sku_price, product_sku.label AS sku_label from `products` left join `product_sku` on `products`.`id` = product_sku.product_id AND product_sku.is_primary = "Yes" AND products.is_variation = "Yes" where `product_name` = "testproduct1" and `status` = "Active" and exists (select * from `campaign` where `products`.`exclusive_for` = `campaign`.`id` and `status` = "Published" and `active_inactive` = "Active" or `products`.`exclusive_for` is null) group by `products`.`id` having id != 0)

union

(select products.*, ROUND ( (LENGTH(products.product_name)- LENGTH( REPLACE ( products.product_name, "testproduct1", "") ) ) / LENGTH("testproduct1") ) AS count1, product_sku.price AS sku_price, product_sku.label AS sku_label from `products` left join `product_sku` on `products`.`id` = product_sku.product_id AND product_sku.is_primary = "Yes" AND products.is_variation = "Yes" where (`products`.`product_name` LIKE "%testproduct1%" or `products`.`description` LIKE "%testproduct1%") and `status` = "Active" and exists (select * from `campaign` where `products`.`exclusive_for` = `campaign`.`id` and `status` = "Published" and `active_inactive` = "Active" or `products`.`exclusive_for` is null) having id != 0 )
group by `p_id`

请帮帮我。

【问题讨论】:

  • 不工作是什么意思?
  • 它给了我语法错误。
  • 然后将错误信息添加到您的问题中
  • 请添加您的错误。以便我们轻松跟踪您的实际问题。

标签: mysql laravel


【解决方案1】:

试试这个

(select products.*,
sum(100000) as count1, 
product_sku.price AS sku_price,
product_sku.label AS sku_label 
from `products`
left join `product_sku` on `products`.`id` = product_sku.product_id 
AND product_sku.is_primary = "Yes" AND products.is_variation = "Yes"
where `product_name` = "testproduct1" and `status` = "Active"
and exists (select * from `campaign` where `products`.`exclusive_for` = `campaign`.`id` and `status` = "Published" and `active_inactive` = "Active" or `products`.`exclusive_for` is null) group by `products`.`id` having id != 0)

union

(select * from 
(select products.*,
ROUND ( (LENGTH(products.product_name)- LENGTH( REPLACE ( products.product_name, "testproduct1", "") ) ) / LENGTH("testproduct1") ) AS count1,
product_sku.price AS sku_price,
product_sku.label AS sku_label
from `products`
left join `product_sku` on `products`.`id` = product_sku.product_id 
AND product_sku.is_primary = "Yes" AND products.is_variation = "Yes" 
where (`products`.`product_name` LIKE "%testproduct1%" or `products`.`description` LIKE "%testproduct1%") and `status` = "Active"
and exists (select * from `campaign` where `products`.`exclusive_for` = `campaign`.`id` and `status` = "Published" and `active_inactive` = "Active" or `products`.`exclusive_for` is null) having id != 0 )A
group by A.`p_id`)

【讨论】:

  • #1054 - 'group statement' 中的未知列 'A.p_id'
猜你喜欢
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 2017-06-09
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
相关资源
最近更新 更多