douli8428 2017-02-01 09:14
浏览 68

在PHP MySQL上显示,加入,计数,分组6个表

i have a problem when showing, join, and grouping 6 tables with php and mysql.

My Table

  • loker_job: id_job, slug_job, position, department, level, location

  • loker_department: id_department, name_department

  • loker_level: id_level, name_level

  • loker_location: id_location, name_location

  • loker_applicant: id_job, file

  • loker_user: id_user, name

My Query

SELECT 
loker_job.slug_job,
loker_level.name_level,
loker_location.name_location,
loker_department.name_department,
loker_user.name,
loker_applicant.file,
count(*) as total_applicant
FROM
loker_job
LEFT JOIN loker_department ON loker_department.id_department =    loker_job.department
LEFT JOIN loker_level ON loker_level.id_level = loker_job.`level`
LEFT JOIN loker_location ON loker_location.id_location = loker_job.location
LEFT JOIN loker_user ON loker_user.id_user = loker_job.publisher
LEFT JOIN loker_applicant ON loker_applicant.id_job = loker_job.slug_job
GROUP by loker_job.slug_job

I want the result like this:

+----------+-----------------+-----------------+---------------+----------------------+-----------------+
| slug_job | name_level      | name_department | name_location | name_position        | total applicant |
+----------+-----------------+-----------------+---------------+----------------------+-----------------+
| 1111     | General Manager | Marketing       | America       | Vice General Manager | 3               |
| 2222     | E-officer       | Ecommerce       | Japan         | Officer              | 1               |
| 3333     | General Manager | Sales           | Rusia         | Vice General Manager | 0               |
| 4444     | General Manager | Marketing       | America       | Vice General Manager | 3               |
+----------+-----------------+-----------------+---------------+----------------------+-----------------+

Result

but, the result i get are:

  • There are one or more row always result 1 total applicant although from db show 0 or nothing
  • the total row not correct, it always -1 from total data

any help will be so appreciated, thank you

  • 写回答

1条回答 默认 最新

  • dtx63505 2017-02-01 10:06
    关注

    First grou by "loker_user" table and left outer join with relational table. And as per below described:

    SELECT loker_job.slug_job, loker_level.name_level, loker_location.name_location,
    loker_department.name_department, A.name, loker_applicant.file, IFNULL(A.total_applicant,0) as total_applicant
    FROM loker_job
    LEFT OUTER JOIN loker_department ON loker_department.id_department =   loker_job.department
    LEFT OUTER JOIN loker_level ON loker_level.id_level = loker_job.`level`
    LEFT OUTER JOIN loker_location ON loker_location.id_location = loker_job.location
    LEFT OUTER JOIN loker_applicant ON loker_applicant.id_job = loker_job.slug_job
    LEFT OUTER JOIN (
    select id_user,name, count(*) as total_applicant from loker_user group by id_user, name
    ) as A ON A.id_user = loker_job.publisher
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大