Php 如何编辑我当前的mySQL查询以获得首选输出?

Php 如何编辑我当前的mySQL查询以获得首选输出?,php,mysql,Php,Mysql,我有下表,我想选择所有 有一个类型为3且类型为form的行 所有类型为_id等于5且类型为action的行 我知道我错了,但我该如何编辑我的查询 桌子 user --- type--- type_id abc ---- form --- 3 abc ---- action--- 5 abc ---- action--- 3 abc ---- form --- 6 当前查询 Select user from table where type = 'form' type = 'act

我有下表,我想选择所有

  • 有一个类型为3且类型为form的行
  • 所有类型为_id等于5且类型为action的行
我知道我错了,但我该如何编辑我的查询

桌子

user --- type--- type_id
abc ---- form --- 3
abc ---- action--- 5
abc ---- action--- 3
abc ---- form --- 6
当前查询

Select user 
  from table 
 where type = 'form' type = 'action' 
   and type_id IN(3, 5)
 Group
    By user
Having count(1) = 2;

您只需将
type
type\u id
的条件配对,然后使用或:

SELECT user FROM table 
    WHERE (type = 'form'   AND type_id = 3)
    OR    (type = 'action' AND type_id = 5)
    GROUP BY user HAVING count(1) = 2;

我没有金[mysql]或[sql]徽章是有原因的(我不知道计数(1)=2是什么)。关闭以签出。

以下查询将给出您上述条件的所有用户名

SELECT t1.user FROM table_name t1 inner join table_name t2 
WHERE (t1.type = 'form' AND t1.type_id = 3) and
(t2.type = 'action' AND t2.type_id = 5)
GROUP BY t1.user;

这里是(

其中(type='form'或type='action')和在(3,5)中键入id
或者是一致的
其中键入('form','action')和在(3,5)中键入id
@abracadver我理解你在那里做了什么,但是我更新了我的问题。原因是,一项行动的类型可能与形式的类型相同,你可以通过两个不同的答案来判断,你的问题,尤其是期望的结果是不清楚的。请确保您清楚地描述了条件,并提供了示例输出。您放弃了吗?我希望(type='form'和type_id=3)和(type='action'和type_id=5)是不可能的。您需要x和y,但在本例中需要选择using OR,因为
type
不能同时是form和action。两种方法都尝试一下,您就会看到。看起来对我来说是正确的<代码>计数(1)与
count(*)
相同。条件也可能是(('form',3),('action',5))
(type,type_id)
-但可能无法通过引擎进行优化。