SQL Query没有值

时间:2018-05-01 08:54:16

标签: mysql sql

我有一张表格如下:

------+-------------+------------+----------------+------------------------------------------------+
| id   | customer_id | date       | action_type_id | details                                        |
+------+-------------+------------+----------------+------------------------------------------------+
| 4225 |         324 | 2015-09-07 |              1 | Sent mail Malcolm Murrey                       |
| 6320 |         324 | 2017-05-08 |              3 | quotes for price.                              |
|  156 |         326 | 2013-07-25 |              3 | Site visit to price job                        |
|  943 |         326 | 2013-10-23 |              1 | Arranged visit for snags on panel              |
| 1135 |         326 | 2013-11-28 |              1 | Arranged visit for site mod                    |
| 1930 |         326 | 2014-04-15 |              2 | Quoted for new HMI                             |
| 2644 |         326 | 2014-10-20 |              2 | Sent email about pending quote for HMI         |
| 2821 |         326 | 2014-11-25 |              1 | Screen problem                                 |
| 2184 |         328 | 2014-07-21 |              1 | Sent email detailing services                  |
+------+-------------+------------+----------------+------------------------------------------------+

我试图找到customer_id没有action_type_id的客户。在这种情况下324和328但似乎无法搞清楚。

我以为我可以做group by customer_id not having action_type_id = 2之类的事情....任何帮助都非常感激。

5 个答案:

答案 0 :(得分:2)

我会使用聚合:

@foreach (var question in Model.AppraisalQuestions)
{
    <p>@question.QuestionDescription</p>
    <div class="row lead evaluation">
        <i class="glyphicon glyphicon-star-empty starrr ratable" data-value="1" data-question="@question.QuestionID"></i>
        <i class="glyphicon glyphicon-star-empty starrr ratable" data-value="2" data-question="@question.QuestionID"></i>
        <i class="glyphicon glyphicon-star-empty starrr ratable" data-value="3" data-question="@question.QuestionID"></i>
        <i class="glyphicon glyphicon-star-empty starrr ratable" data-value="4" data-question="@question.QuestionID"></i>
        <i class="glyphicon glyphicon-star-empty starrr ratable" data-value="5" data-question="@question.QuestionID"></i><br />
        <span class="count">0</span> star(s) - <span class="meaning"> </span>
    </div><br />
}

我发现使用<script type="text/javascript"> $(document).ready(function () { var correspondence = ["", "Poor", "Below Expectation", "Above Expectation", "Good", "Excelent"]; $(".ratable").click(function() { var value = $(this).data("value"); var questionID = $(this).data("question"); $(this).closest('.evaluation').children('.count').text(value); $(this).closest('.evaluation').children('.meaning').html(correspondence[value]); //TODO: send value and questionID to server, perhaps via ajax or something }); }); </script> select customerid from t group by customerid having sum(action_type_id = 2) = 0; 这种类型的查询在您可能拥有的条件上非常灵活(比如你想要2和3,或者不是2但是3或4 )。

答案 1 :(得分:1)

您可以通过在ID为2的记录上左连接到自身来执行此操作,然后对没有匹配的记录进行过滤。

的内容
SELECT DISTINCT customer_id 
FROM customer a
LEFT JOIN customer b
ON a.customer_id = b.customer_id
AND b.action_type_id = 2
WHERE b.customer_id IS NULL

答案 2 :(得分:1)

为什么不使用not exist

select * 
from table t
where not exists (select 1 from table 
                  where customer_id  = t.customer_id and 
                        action_type_id = 2);

答案 3 :(得分:0)

为什么不这样,

select distinct customer_id from customer where  action_type_id <> 2

答案 4 :(得分:0)

这应该有效:

SELECT customer_id FROM customers WHERE action_type_id <> 2