Php 如何在不重复的情况下选择union 2
迭代问题 在使用codeigniter Mysql的单个查询中使用codeigniter可以获得此输出吗Php 如何在不重复的情况下选择union 2,php,mysql,database,codeigniter,phpmyadmin,Php,Mysql,Database,Codeigniter,Phpmyadmin,迭代问题 在使用codeigniter Mysql的单个查询中使用codeigniter可以获得此输出吗 oid | count(waiters) as total_waiters ----+------------------------------- 1 | 1 <-- john will be count as 1 even if assigned to 2 room 2 | 1 3 | 2 <-- count is 2 becaus
oid | count(waiters) as total_waiters
----+-------------------------------
1 | 1 <-- john will be count as 1 even if assigned to 2 room
2 | 1
3 | 2 <-- count is 2 because different waiters are assigned with different room
4 | 0
餐桌
oid | name
----+-------
1 | aa
2 | bb
3 | cc
4 | dd
Rid | oid | waiter_assigned
----+-------+-----------------
1 | 1 | john
2 | 1 | john
3 | 2 | john
4 | 3 | mike
5 | 3 | dude
我试着使用union
$this->db->select('o.oid, "" AS tot_book_thera');
$this->db->from('order o');
$query1 = $this->db->get_compiled_select();
$this->db->select('r.oid, count(r.waiter_assigned) AS total_waiters');
$this->db->from('room r');
$this->db->group_by('r.waiter_assigned');
$query2 = $this->db->get_compiled_select();
但我明白了
oid | count(waiters) as total_waiters
----+-------------------------------
1 | 1
2 | 1
3 | 2
1 | '' <-- not sure how to combine it with the 1st or how to exclude this or remove this...
非常感谢大家的帮助,谢谢大家 你的想法是对的。但正如其他人所说,GROUPBY是你在这里最好的朋友。另外,使用DISTINCT可以避免为同一份订单数服务员两次。这就是您的代码应该是什么样子
// An inner select query whose purpose is to count all waiter per room
// The key here is to group them by `oid` since we are interested in the order
// Also, in the count(), use DISTINCT to avoid counting duplicates
$this->db->select('room.oid, count(DISTINCT room.waiter_assigned) AS total_waiters');
$this->db->from('room');
$this->db->group_by('room.oid');
$query1 = $this->db->get_compiled_select();
// If you run $this->db->query($query1)->result(); you should see
oid | total_waiters
----+-------------------------------
1 | 1
2 | 1
3 | 2
// This is how you would use this table query in a join.
// LEFT JOIN also considers those rooms without waiters
// IFNULL() ensures that you get a 0 instead of null for rooms that have no waiters
$this->db->select('order.oid, order.name, IFNULL(joinTable.total_waiters, 0) AS total_waiters');
$this->db->from('order');
$this->db->join('('.$query1.') joinTable', 'joinTable.oid = order.oid', 'left');
$this->db->get()->result();
// you should see
oid | name | total_waiters
----+-----------+-------------------------
1 | aa | 1
2 | bb | 1
3 | cc | 2
4 | dd | 0
下面是原始SQL语句
SELECT order.oid, order.name, IFNULL(joinTable.total_waiters, 0) AS total_waiters
FROM order
LEFT JOIN (
SELECT room.oid, count(DISTINCT room.waiter_assigned) AS total_waiters
FROM room
GROUP BY room.oid
) joinTable ON joinTable.oid = order.oid
你应该读到关于分组方式的内容。。正在查看选择'o.*,并按'o.oid'分组\u。。。选择'o.*,group_by'r.waterr_assigned'告诉我,您不知道它可能有什么问题,也不知道如何在SQL语言中正确使用group by。@RaymondNijland我理解,但如果我应该使用group by或其他代码,我不知道该用什么了……这些查询有效的唯一方法是在函数依赖性可以被删除时习惯于无论如何,让我看看我是否理解输出后面的逻辑。@RaymondNijland hmmm好的,那么如果我删除我的组,会有可能的解决方案吗?感谢您的帮助,如果需要,我将更新我的标题和其他详细信息,以消除混淆需要先做其他事情无论如何,这里要获得结果集的主要问题是您需要加入,并在oid上取消与GROUP BY的复制,因为您需要使用COUNT。。除此之外,从MySQL的角度来看,oid和Rid之间并没有一个明确的分离,这就是为什么不将分离计算在内。。但我现在需要走几个小时,不必花时间来格式化整个查询和回答,但请考虑一下“谢谢”,它确实起作用了!想知道它是否能比makeforloop更快地获取服务生总数……对于像这样的一个小查询,性能其实并不重要。我建议您使用DBMS,因为DBMS旨在优化连接。使用PHP需要使用循环和if-else语句,您必须自己维护和优化这些语句。