使用 union 创建 zf2 query 'not in' 子句
create zf2 query 'not in' clause using uninon
我想在 ZF2 中使用联合查询结果 Not in
。此查询的结果将 return
Query: $selectLevelTwoPointFive->combine($selectLevelAll, 'UNION');
Result: ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items_level_twopointfive` AS `assigned` ) UNION ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items` AS `assigned` )
现在我想在 'not in' 子句中使用此查询的结果。
$select = $sql->select()->from(array(
"items" => "cu_items"
))->columns(array('item_name'=> new \Zend\Db\Sql\Expression(" group_concat(`items`.`item_name`)") ));
$select->join(array(
"ca" => "cu_areas"
), new Expression(" ca.area_id = items.area_id ")
, array(
'area_name'
), $selectLevelAll::JOIN_INNER);
$select->where->addPredicate(new \Zend\Db\Sql\Predicate\Expression('items.item_id NOT IN (?)',
array($selectLevelTwoPointFive)));
结果是:
SELECT group_concat(`items`.`item_name`) AS `item_name`, `ca`.`area_name` AS `area_name` FROM `cu_items` AS `items` INNER JOIN `cu_areas` AS `ca` ON ca.area_id = items.area_id WHERE `items`.`is_opted` = 'yes' AND `ca`.`is_opted` = 'yes' AND items.item_id NOT IN ((( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items_level_twopointfive` AS `assigned` ) UNION ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items` AS `assigned` )))
显示 mysql 错误:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION ( SELECT
assigned.
item_idAS
item_idFROM
assign_itemsAS
assigne' 在第 1 行`
我需要的查询是:
SELECT group_concat(items.item_name) AS item_name, ca.area_name AS area_name FROM cu_items
AS items INNER JOIN cu_areas AS ca ON ca.area_id = items.area_id
WHERE items.is_opted = 'yes' AND ca.is_opted = 'yes' AND items.item_id NOT IN
(
(
SELECT assigned.item_id AS item_id FROM assign_items_level_twopointfive AS assigned
UNION ( SELECT assigned.item_id AS item_id FROM assign_items AS assigned )
)
)
实际上,当我使用 combine() 时,它会添加圆括号(第一个查询)联合(第二个查询)但是 in not
我需要第一个查询联合第二个查询。
请帮忙。如果有人建议 mysql 更改它也很好。
感谢上帝,找到页面。
我们需要覆盖 setSpecification()
这是 link 的答案。
we can override the query output
我想在 ZF2 中使用联合查询结果 Not in
。此查询的结果将 return
Query: $selectLevelTwoPointFive->combine($selectLevelAll, 'UNION');
Result: ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items_level_twopointfive` AS `assigned` ) UNION ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items` AS `assigned` )
现在我想在 'not in' 子句中使用此查询的结果。
$select = $sql->select()->from(array(
"items" => "cu_items"
))->columns(array('item_name'=> new \Zend\Db\Sql\Expression(" group_concat(`items`.`item_name`)") ));
$select->join(array(
"ca" => "cu_areas"
), new Expression(" ca.area_id = items.area_id ")
, array(
'area_name'
), $selectLevelAll::JOIN_INNER);
$select->where->addPredicate(new \Zend\Db\Sql\Predicate\Expression('items.item_id NOT IN (?)',
array($selectLevelTwoPointFive)));
结果是:
SELECT group_concat(`items`.`item_name`) AS `item_name`, `ca`.`area_name` AS `area_name` FROM `cu_items` AS `items` INNER JOIN `cu_areas` AS `ca` ON ca.area_id = items.area_id WHERE `items`.`is_opted` = 'yes' AND `ca`.`is_opted` = 'yes' AND items.item_id NOT IN ((( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items_level_twopointfive` AS `assigned` ) UNION ( SELECT `assigned`.`item_id` AS `item_id` FROM `assign_items` AS `assigned` )))
显示 mysql 错误:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION ( SELECT
assigned.
item_idAS
item_idFROM
assign_itemsAS
assigne' 在第 1 行`
我需要的查询是:
SELECT group_concat(items.item_name) AS item_name, ca.area_name AS area_name FROM cu_items
AS items INNER JOIN cu_areas AS ca ON ca.area_id = items.area_id
WHERE items.is_opted = 'yes' AND ca.is_opted = 'yes' AND items.item_id NOT IN
(
(
SELECT assigned.item_id AS item_id FROM assign_items_level_twopointfive AS assigned
UNION ( SELECT assigned.item_id AS item_id FROM assign_items AS assigned )
)
)
实际上,当我使用 combine() 时,它会添加圆括号(第一个查询)联合(第二个查询)但是 in not
我需要第一个查询联合第二个查询。
请帮忙。如果有人建议 mysql 更改它也很好。
感谢上帝,找到页面。
我们需要覆盖 setSpecification()
这是 link 的答案。
we can override the query output