在一个查询中按计数和运算符获取结果
Posted
技术标签:
【中文标题】在一个查询中按计数和运算符获取结果【英文标题】:Get results by count and operators in one query 【发布时间】:2017-02-21 06:26:15 【问题描述】:我认为问题是错误的,但我不知道如何正确提问。
此查询选择所有工作人员的所有企业,其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 的回答有点用,但它没有根据需要显示结果。
【问题讨论】:
那么哪一列包含工人数?然后只需在 whereRaw 中执行worker_count > 5
大于 5 等。
数据库中没有存储计数的列
@MattCowley 我猜有一个 Worker 表和一个 Businesstable 和一个 Real 表。像workerA 一样的不动产ia |商务 B。你必须在你的第一次加入时按 b_id 和 Count(workers) 分组。
为什么不只是->where('job_types', 'like', '%C%')
而不是->whereRaw()
?
@Neat 因为真正的查询比这里更复杂。在这里我只发布了小而易读的查询。
【参考方案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 中的 cmets。
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. r_id (for auto increment) 2. b_id 3. w_id
。我现在就试试你的代码
看起来,您的代码运行良好,但我有一个,最后一个问题。例如:t4.workers_count = 3
仅显示一行,如果该企业有 3 名员工,"%C%"
。如何将该公司名称复制 3 次并存储这 3 个工人信息?您的查询运行良好,但这个小改动将使这个查询成为传奇。
看起来我是通过更改 group by
自己制作的,为了验证我添加了 order by name
。谢谢,如果可能的话,让我们继续保持联系。
好的,所以问题还是存在,想不通,如何显示正确的结果。如果t4.wokers_count = 3
它显示所有拥有 3 名工人的企业"%C%"
。如何将已创建的业务复制 3 次,并在每个复制行中显示一名工人的信息。查看更新的问题。【参考方案2】:
$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();
【讨论】:
我认为您在 selectRaw 行中犯了一个错误。没有引号 对不起,它给了我一个错误的 SQL 语法错误。You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '*)
.
它正在工作,但看起来运算符不起作用,因为它给我的结果不正确。例如,如果我设置 workers_count < 3
它仍然返回具有例如 6 个工人的企业。以上是关于在一个查询中按计数和运算符获取结果的主要内容,如果未能解决你的问题,请参考以下文章