在同一列上获取具有不同数据的行

时间:2017-01-27 16:39:45

标签: php mysql sql

我们有像

这样的表格结构
Fieldid userid data
41       2      1
12       2      0
41       3      1
12       3      1

我们希望用户拥有(data = 1和filedid = 41)和(data = 0 and filedid = 12)

我们尝试了查询

select userid from table 
where (data=1 and filedid = 41) 
AND (data = 0 and filedid=12)

但它返回空结果。 任何人都可以帮助我们如何使用MySQL获取记录吗?

4 个答案:

答案 0 :(得分:1)

您可以在HAVING子句中使用条件聚合进行分组:

SELECT userid
FROM mytable
GROUP BY userid
HAVING COUNT(CASE WHEN data = 1 AND FieldId = 1 THEN 1 END) >= 1 AND
       COUNT(CASE WHEN data = 0 AND FieldId = 12 THEN 1 END) >= 1

答案 1 :(得分:0)

与@Giorgos类似,多列比较简洁:

select userid
from t
group by userid
having  sum((data,fieldId) = (1, 41)) > 0
    and sum((data,fieldId) = (0, 12)) > 0

答案 2 :(得分:0)

您需要使用union all,例如:

select * from table1 where Fieldid = 41 and data = 1 union all
select * from table1 where Fieldid = 12 and data = 0

附图显示输出

enter image description here

答案 3 :(得分:-1)

使用一组或另一组数据搜索结果时,您必须使用//Create a list containing all the elements from the callendar. IList<WebElement> listOfElements = new IList<WebElement>(); listOfElements = driver.FindElements(By.XPath("//div[@class='rc-row rc-widget-header']/table/thread/tr/th")); //Search for the one that matches the element that you are looking for. foreach(IWebElement element in listOfElements){ if(element.Text.equals("Sun 1/22"){ element.Click(); } } 关键字,而不是OR

AND