且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 sql 中区分 foreach 循环中的重复项

更新时间:2022-06-01 03:10:56

替代方案 - 让数据库为您完成工作:

Alternative - let the database do the work for you:

SELECT j.*, c.appl_count FROM jp_applications j
    INNER JOIN (SELECT user_id, count(1) as appl_count FROM jp_applications
            WHERE application_status = "Awaiting Response"
            GROUP BY user_id) c on c.user_id = j.user_id
WHERE j.application_status = "Awaiting Response"
ORDER BY j.job_id

然后您的结果集将有可用的字段appl_count",如果大于 1,则附加该类.这消除了在应用代码中进行任何针锋相对的会计处理的需要.

Then your resultset will have the field 'appl_count' available, if greater than 1, append the class. This removes the need to do any tit-for-tat accounting in the app code.