Tau*_*ras 6 php sql foreach count laravel
我认为问题是错误的,但我不知道如何正确提问.
此查询选择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();
Run Code Online (Sandbox Code Playgroud)
我正在使用foreach来显示结果
foreach ($connect as $item) {
echo $item->name;
// etc
}
Run Code Online (Sandbox Code Playgroud)
我想选择所有超过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``
Run Code Online (Sandbox Code Playgroud)
回答@KikiTheOne有点工作,但它不会根据需要显示结果.
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
未格式化
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
Run Code Online (Sandbox Code Playgroud)
旧代码
关于帖子 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
Run Code Online (Sandbox Code Playgroud)
。
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
Run Code Online (Sandbox Code Playgroud)
代码未格式化:
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
Run Code Online (Sandbox Code Playgroud)
让我知道这是否对你有帮助
| 归档时间: |
|
| 查看次数: |
186 次 |
| 最近记录: |