选择除第一个以外的所有记录

时间:2011-02-15 12:40:06

标签: mysql

我有一个表格可以容纳一个客户,另一个表格可以保存该客户的结果。

一位客户可以获得很多结果。

我希望能够首先只选择在CustResults中有多个条目的客户,然后从那些客户那里获得除第一个以外的所有记录......

到目前为止,我有这个,它检索了多个结果的客户,但我不知道如何然后进入第一个结果。

SELECT * FROM CustResults cp 
JOIN Customer c ON c.CustomerID = cp.CustomerID
WHERE
(SELECT count(CustomerID) as cpid 
 FROM CustResults WHERE CustomerID = cp.CustomerID GROUP BY CxID) > 1

丽塔:结果1 苏:结果1,结果2,结果3,结果4 Bob:结果1,结果2,结果3

我只想要Sue和Bob,因为Rita只有一个结果,而Sue和Bob,我只想看结果2,3,4

有什么想法吗?

由于

添加更多信息:

这是我的确切查询:

SELECT count(cp.CxID)as intSmokers FROM CustPrimarySmoking cp JOIN Customer c ON cp.CxID = c.CustomerID WHERE(SELECT count(CustPrimarySmokingID)as cqpid FROM CustPrimarySmoking WHERE CxID = cp.CxID GROUP BY CxID)> 1

显然,我可以使用LIMIT 1,99999,因为查询只返回一个值(计数)。

我希望计数在CustPrimarySmoking中使用具有多个记录的客户,但忽略第一个条目。

任何进一步的想法?

3 个答案:

答案 0 :(得分:2)

愚蠢而且不那么灵活(但是,它应该在大多数情况下 all )......

LIMIT 1, 999999999;
         ^ to ensure all rows are returned

答案 1 :(得分:0)

您是否尝试在查询结尾添加“OFFSET 1”?

答案 2 :(得分:0)

如果您所追求的只是计数而不是实际记录,那么您只需要从已有的计数中减去客户数量(因为您已经确定每个客户至少有一条记录)即

SELECT count(cp.CxID)-count(DISTINCT cp.CxID) as intSmokers
FROM CustPrimarySmoking cp
JOIN Customer c ON cp.CxID = c.CustomerID
WHERE (
  SELECT count(CustPrimarySmokingID) as cqpid
  FROM CustPrimarySmoking WHERE CxID = cp.CxID
  GROUP BY CxID
  ) > 1

但是,如果您在实际行之后,那么:

    SELECT * FROM Customer;
    +------------+------+
    | CustomerID | name |
    +------------+------+
    |          1 | Rita |
    |          2 | Sue  |
    |          3 | Bob  |
    |          4 | Jack |
    +------------+------+


    SELECT * FROM CustPrimarySmoking;

    +----------------------+------+-------------------+
    | CustPrimarySmokingID | CxID | result            |
    +----------------------+------+-------------------+
    |                    1 |    1 | Result 1 for Rita |
    |                    2 |    2 | Result 1 for Sue  |
    |                    3 |    2 | Result 2 for Sue  |
    |                    4 |    2 | Result 3 for Sue  |
    |                    5 |    2 | Result 4 for Sue  |
    |                    6 |    3 | Result 1 for Bob  |
    |                    7 |    3 | Result 2 for Bob  |
    |                    8 |    3 | Result 3 for Bob  |
    +----------------------+------+-------------------+

    SELECT * FROM CustPrimarySmoking cp
    JOIN Customer c ON cp.CxID = c.CustomerID
    WHERE CustPrimarySmokingID <> (
        SELECT CustPrimarySmokingID
        FROM CustPrimarySmoking
        WHERE CxID = cp.CxID ORDER BY CustPrimarySmokingID LIMIT 1
    );


    +----------------------+------+------------------+------------+------+
    | CustPrimarySmokingID | CxID | result           | CustomerID | name |
    +----------------------+------+------------------+------------+------+
    |                    3 |    2 | Result 2 for Sue |          2 | Sue  |
    |                    4 |    2 | Result 3 for Sue |          2 | Sue  |
    |                    5 |    2 | Result 4 for Sue |          2 | Sue  |
    |                    7 |    3 | Result 2 for Bob |          3 | Bob  |
    |                    8 |    3 | Result 3 for Bob |          3 | Bob  |
    +----------------------+------+------------------+------------+------+