php - 如何组合两个 MySQL 查询,其中一个查询在另一个 while 循环中定义?

标签 php mysql

我正在合并 2 个查询。我可以从对话列表中选择*,使用对话列表。*,但我不确定如何将employee_id='$rowemployees[employee_id]'放入我顶部的主查询中?它结合了 PHP 和 MySQL,但都可以变成 MySQL。

$sqlemployees = mysqli_query('select u.fname, u.lname, d_e.*, d_list.*, d_list.emplsub.sqlcal AS sqlcalcemp 
from 
(select count(*) AS sqlcal from  dialogue_employees d_e, 
dialogue_leaders d_l 
  where
  d_l.leader_group_id = d_e.leader_group_id and
  d_l.cycle_id = $cycle_id) AS sub,
dialogue_list d_list, user u, dialogue_employees d_e, 
dialogue_leaders d_l 
where employee_id=d_list.employee_id and
u.userID = d_e.employee_id and 
d_l.leader_group_id = d_e.leader_group_id and
d_l.cycle_id = $cycle_id') or die(mysql_error());

while($rowemployees=mysqli_fetch_array($sqlemployees))
{
  $sqli=mysqli_query("select * 
  from dialogue_list 
  where employee_id='$rowemployees[employee_id]' and cycle_id='$cycle_id'")or die(mysql_error());
}

最佳答案

你必须做一个左连接,我发现并查看表如何相互交互(这就是我必须组合cycle_id的地方。选择* Dialogue_list是d_list。*然后它看到组合的内容员工 ID。

select u.fname, u.lname, d_e.*, d_list.*, subb.sqlcal AS sqlcalemp 
from (select count(*) as sqlcal from  dialogue_employees d_e, 
    dialogue_leaders d_l 
where
    d_l.leader_group_id = d_e.leader_group_id and
    d_l.cycle_id = $cycle_id) as subb, 
user u, dialogue_employees d_e 
LEFT JOIN dialogue_list d_list ON d_e.employee_id = d_list.employee_id, 
    dialogue_leaders d_l where
    u.userID = d_e.employee_id and 
    d_l.leader_group_id = d_e.leader_group_id and
    d_l.cycle_id = $cycle_id and d_list.cycle_id = $cycle_id

关于php - 如何组合两个 MySQL 查询,其中一个查询在另一个 while 循环中定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655233/

相关文章:

mysql - 我如何实现具有两行的 RecyclerView,其中每行从不同的对话框(DIALOG_A,DIALOG_B)获取数据?

php - 在项目值的每个现有首字母处添加额外的 <li>

php - MySQL 数据未出现在工具提示中

php - 解析包含 "href"标记中特定单词的所有链接

PHPUnit,在我对抽象类及其子类的测试中避免重复的正确方法

mysql - Galera_new_cluster 命令未启动集群

PHP system() 使用 MySQL 在许多数据库上运行查询

java - 在java应用程序中生成12位唯一编号

PHP+MySQL join 语句将多个值排列为 mysql_fetch 数组中的行

php - 从不同的页面文件中显示和隐藏 DIV 类