在表格中搜索先前是否存在记录

时间:2012-02-11 13:18:53

标签: sql

我有一个orders表。我想搜索客户是否第一次订购。我们可以在订单表中使用email_id字段来检查它。但是如何使用SQL查询检查它?也就是说,我想搜索特定客户的第一个订单。我该怎么办?

3 个答案:

答案 0 :(得分:0)

使用以下查询

select * from orders where email_id='myid@mysite.com'

如果此查询中有数据,则表示特定客户未首次订购...

希望这就是你想要的......

更新1

试试这个......

select emailid, count(emailid) as total from order where 1=1 group by emailid order by total

这将为您提供以下输出

emailid       total
++++++++++++++++++++
my1@mm.com      1
my2@mm.com      4
my3@mm.com      6

同时检查select emailid, count(emailid) as total from order where total>1 group by emailid order by total的内容 由于我这里没有sql,我无法检查:(

祝你好运

答案 1 :(得分:0)

试试这个:

SELECT email_id, IF(COUNT(email_id) > 0, 'NOT', 'First Time') AS Status
FROM `Orders`
WHERE email_id = 'EMAILADDRESSHERE'
GROUP BY email_id

答案 2 :(得分:0)

第一顺序:

T-SQL:

SELECT TOP 1 * 
FROM orders 
WHERE email_id='myid@mysite.com'
ORDER BY order_date DESC

PostgreSQL / MySQL:

SELECT 
FROM orders 
WHERE email_id='myid@mysite.com'
ORDER BY order_date DESC
LIMIT 1

Interbase的:

SELECT FIRST 1 SKIP 0 
FROM orders 
WHERE email_id='myid@mysite.com'
ORDER BY order_date DESC