gpt4 book ai didi

php - SQL 查询 - 获取行退出其他表 laravel 上的一些值

转载 作者:行者123 更新时间:2023-11-29 07:37:04 24 4
gpt4 key购买 nike

我有一个问题

$cartable = DB::table('requests')
->select('requests.*','crequest.*')
->leftJoin('crequest','crequest.id','=','requests.request_id')
->whereRaw("FIND_IN_SET('$userid', crequest.user_allows)")
->orderBy('forms_cartable.id','desc')->get();

我的表:

requests:
+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
| 3 | test3 | low | process |
+------+--------------+------------------+-------------------+


crequest
+------+-------------+----------------+--------------+
| id | request_id | users_allow | c_status |
+------+-------------+----------------+--------------+
| 3 | 1 | 12,13,15 | done |
| 4 | 1 | 12 | done |
| 5 | 1 | 13 | end |
| 6 | 2 | 42,12,35 | done |
| 7 | 2 | 47,65 | done |
| 8 | 3 | 42 | open |
+------+-------------+----------------+--------------+

我想如果有一个现有的当前用户 ID(Logged) 在 crequest.user_allows 中仅返回请求而没有 crequest 详细信息

看起来像这样:

(如果当前用户 ID 为 12)请求表结果(执行查询后)

+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
+------+--------------+------------------+-------------------+

但是查询结果所有记录在表crequest中匹配我只需要请求行如果在crequest表中匹配

最佳答案

我不知道 php 但您可以通过多种方式在 MySql 中实现它。这是其中的3种方式..

方法一:

SELECT DISINCT R.*
FROM requests R
INNER JOIN crequest CR ON R.id = CR.request_id
WHERE FIND_IN_SET ('12', CR.users_allow);

方法二:

SELECT *
FROM requests R
WHERE EXISTS (SELECT 1 FROM crequest CR WHERE R.id = CR.request_id
AND FIND_IN_SET ('12', CR.users_allow));

方法三:

SELECT *
FROM requests R
WHERE R.id in (SELECT request_id FROM crequest WHERE FIND_IN_SET ('12', CR.users_allow))

关于php - SQL 查询 - 获取行退出其他表 laravel 上的一些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com