我有专栏:
code unique date info name
a123 adfgh 2014-07-14 14:15:00 test user1
a124 aklgp 2014-08-12 09:15:00 test user1
a175 opllp 2014-08-12 11:15:00 test user2
我希望获得所有记录,其中unique = $ variable但在那一天。 Exmpl: 如果我的独特=' aklgp'我想看到两个
a124 aklgp 2014-08-12 09:15:00 test user1
a175 opllp 2014-08-12 11:15:00 test user2
因为两个记录都在同一天?它可以吗?
我玩DATEDIFF,但对我的
不好答案 0 :(得分:2)
不确定这是否足够,但这个想法可能就是这样。
cast([date] as date)
将采用" date"你的日期时间的一部分(希望这是一个日期时间类型),这将给你......一天。
select *
from yourTable
where cast([date] as date) IN (select cast([date] as date)
from yourTable
where [unique] = 'aklgp')
当然,如果您在不同的日子有很多aklgp
,您将获得更多数据。但唯一的应该是......独特,没有?
请参阅SqlFiddle
修改强>
如Bulat所述,您也可以使用EXISTS
SELECT * from yourTable t1
where exists (select null
from yourTable t2
where cast(t1.date as date) = cast(t2.date as date)
and t2.[unique] = 'aklgp')
答案 1 :(得分:1)
这可能对你有帮助,
DECLARE @UNIQUE VARCHAR(20)
SET @UNIQUE = 'AKLGP'
SELECT * FROM [TABLE]
WHERE CAST([DATE] AS DATE) = (SELECT CAST([DATE] AS DATE) FROM [TABLE] WHERE [UNIQUE] = @UNIQUE )
答案 2 :(得分:0)
我不知道只使用SQL的解决方案。也许PHP中的解决方案可以提供一些帮助:
$query = "SELECT * FROM yourTable WHERE unique = 'aklgp'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$year = date_format($row['date'], 'Y');
$month = date_format($row['date'], 'm');
$day = date_format($row['date'], 'd');
$query = "SELECT * FROM yourTable
WHERE (DATEPART(yy, date) = $year
AND DATEPART(mm, date) = $month
AND DATEPART(dd, date) = $day)";