如何结合这2个mysql查询?

时间:2021-01-15 01:12:23

Sorry if this is repeated but all the solutions I found did not work, probably because both queries are on the same table.

很抱歉,如果重复此操作,但我找到的所有解决方案都不起作用,可能是因为两个查询都在同一个表中。

This is what I'm doing now.

这就是我现在正在做的事情。

$sql = $database->query("SELECT id FROM attachments WHERE user='$user_id'");
$count = mysql_num_rows($sql);

$sql = $database->query("SELECT id FROM attachments WHERE member_id IN ($found_groups)");
$count += mysql_num_rows($sql);

$found_groups is a string that comes from a previous query

$ found_groups是来自先前查询的字符串

$sql_groups = $database->query("SELECT DISTINCT group_id FROM tbl_members WHERE client_id='$client_id'");

And the result is then imploded

然后结果就会崩溃

Both queries work fine. The problem is that the results are not DISTINCT and that there are 3 queries for each user, which I feel are a lot.

两个查询都运行良好。问题是结果不是DISTINCT,每个用户有3个查询,我觉得很多。

How could I reduce this?

我怎么能减少这个?

Thanks a lot.

非常感谢。

1 个解决方案

#1


0  

Try:

SELECT DISTINCT id FROM attachments 
WHERE user='$user_id' OR member_id IN ($found_groups)

#1


0  

Try:

SELECT DISTINCT id FROM attachments 
WHERE user='$user_id' OR member_id IN ($found_groups)