我认为问题是错的,但我不知道如何正确地提问。
此查询选择包含job_types
包含C
个字母的所有工作人员的所有商家。
$connect = DB::table('relationship')
->join('workers', 'workers.id', '=', 'relationship.w_id')
->join('business', 'business.id', '=', 'relationship.b_id')
->whereRaw('job_types LIKE "%C%"')
->groupBy('relationship.w_id')
->get();
我正在使用foreach来显示结果
foreach ($connect as $item) {
echo $item->name;
// etc
}
我想选择所有超过3或少于3或等于3的商家(取决于我需要的商品)job_types LIKE "%C%"
并存储以下信息:
1. APPLE | Tom | C
2. APPLE | Tim | C
3. APPLE | Jeff | C
4. IBM | Jenny | C
5. IBM | Sean | C
6. IBM | Ian | C
// etc``
通过 @KikiTheOne 回答有点工作,但不会根据需要显示结果。
答案 0 :(得分:1)
SELECT
*
FROM
people_details as t1
inner join
people_branches as t2
on t1.id = t2.id
inner join
(
SELECT
count(t1.id) as worker_counter,t1.branch_id
FROM
people_branches as t1
inner join people_details as t2
on t1.id = t2.id
WHERE
t2.job_types LIKE '%C%'
group by branch_id
) as t3
on t2.branch_id = t3.branch_id
inner join people_frontpage as t4
on t4.id = t1.id
inner join business as t5
on t5.id = t2.branch_id
WHERE
t1.job_types LIKE '%C%'
AND t3.worker_counter > 200
SELECT
t3.bus_name, t1.name, t1.job_types
FROM
SO_WORKER as t1
inner join
SO_RELATIONSHIP as t2
on t1.id = t2.w_id
inner join
(
SELECT
count(t1.w_id) as worker_counter,t1.b_id,t3.bus_name
FROM
SO_RELATIONSHIP as t1
inner join SO_WORKER as t2
on t1.w_id = t2.id
inner join SO_BUSINESS as t3
on t3.id = t1.b_id
WHERE
t2.job_types LIKE '%C%'
group by b_id
) as t3
on t2.b_id = t3.b_id
WHERE t1.job_types LIKE '%C%'
AND t3.worker_counter <= 3
未格式化的
SELECT t3.bus_name, t1.name, t1.job_types FROM SO_WORKER as t1 inner join SO_RELATIONSHIP as t2 on t1.id = t2.w_id inner join (SELECT count(t1.w_id) as worker_counter,t1.b_id,t3.bus_name FROM SO_RELATIONSHIP as t1 inner join SO_WORKER as t2 on t1.w_id = t2.id inner join SO_BUSINESS as t3 on t3.id = t1.b_id WHERE t2.job_types LIKE '%C%' group by b_id) as t3 on t2.b_id = t3.b_id WHERE t1.job_types LIKE '%C%' AND t3.worker_counter <= 3
旧代码
与帖子1中的评论相关。
Table: SO_BUSINESS
id | bus_name
--------------------
1 | BUSI A
2 | BUSI B
Table: SO_WORKER
id | job_types
---------------------
1 | CEO
2 | GFO
3 | CTO
4 | Manager
5 | Worker
Table: SO_RELATIONSHIP
w_id | b_id
----------------
1 | 1
2 | 2
3 | 1
4 | 1
5 | 2
Query: Output
workers_count | b_id | bus_name
--------------------------------------------
2 | 1 | BUSI A
SELECT *
FROM
(
SELECT
count(t1.w_id) as workers_count,
t1.b_id,
t3.bus_name
FROM
SO_RELATIONSHIP as t1
inner join
SO_WORKER as t2 on t1.w_id = t2.id
inner join
SO_BUSINESS as t3 on t1.b_id = t3.id
WHERE
t2.job_types LIKE '%C%'
GROUP BY t1.b_id
) as t4
WHERE
t4.workers_count < 3
代码未格式化:
SELECT * FROM (SELECT count(t1.w_id) as workers_count,t1.b_id,t3.bus_name FROM SO_RELATIONSHIP as t1 inner join SO_WORKER as t2 on t1.w_id = t2.id inner join SO_BUSINESS as t3 on t1.b_id = t3.id WHERE t2.job_types LIKE '%C%' GROUP BY t1.b_id) as t4 WHERE t4.workers_count < 3
如果有帮助,请告诉我
答案 1 :(得分:0)
$connect = DB::table('relationship')
->join('workers', 'workers.id', '=', 'relationship.w_id')
->join('business', 'business.id', '=', 'relationship.b_id')
->selectRaw('workers.*,business.*,(select count(*) from workers where job_types like "%c%") as workers_count')
->where('job_types', 'like', '%C%')
->having('workers_count','>=',5)
->groupBy('relationship.w_id')
->get();