1

Imagine that I have a column like this:

Var: 1, 1, , 3, 2
-
Name: Ben, John, Josh, Bill

How can I select Entries with the same VAR column Value? Like, if I want entries with value 1 in the VAR column, it will give me Ben and Josh.

4

3 回答 3

0

Try

SELECT name
  FROM table1
 WHERE var IN (SELECT MIN(var)
                FROM table1
               GROUP BY var
              HAVING COUNT(*) > 1)

Here is SQLFiddle demo.

于 2013-05-25T01:00:27.100 回答
0

This will give you records having multiple records of the same VAR.

SELECT  a.*
FROM    TableName a
WHERE   EXISTS
        (
            SELECT  1
            FROM    TableName b
            WHERE   a.Var = b.Var
            GROUP   BY Var
            HAVING  COUNT(*) > 1
        )

Another way to solve this is by using JOIN,

SELECT  a.*
FROM    TableName a
        INNER JOIN
        (
            SELECT  Var
            FROM    TableName b
            GROUP   BY Var
            HAVING  COUNT(*) > 1
        ) b ON a.Var = b.Var

But adds a little confusion when you add this line: "..if I want entries with value 1 in the VAR column, it will give me: Ben and Josh" -- Do you want to specify VAR or not? Like this demo <<

于 2013-05-25T01:00:46.647 回答
0

this question is confusing, does a select not work?

select name from theTable
where var = 1
于 2013-05-25T02:04:11.500 回答