日期:2014-05-18  浏览次数:20525 次

求一MYSQL
SQL code

SELECT
ID,
Name,
Time,
IP
FROM
Table
WHERE
time>='2012-01-04 00:00:00' and time<='2012-01-10 23:59:59'



我想在这段基础上加上,所得到的IP是否为2012-01-04前就已经存在的?

------解决方案--------------------
不会吧。
------解决方案--------------------
真的不会吧
------解决方案--------------------
探讨
我想在这段基础上加上,所得到的IP是否为2012-01-04前就已经存在的?

------解决方案--------------------
猜测lz的意思为:想找到IP对应的日期在2012-01-04前就有的

SQL code

use tempdb;
/*
create table A
(
    ID int not null,
    Name nvarchar(10) not null,
    [Time] date not null,
    IP nvarchar(20) not null
);
insert into A values
(1,'a','2012-01-01','192.168.1.1'),
(2,'b','2012-01-01','192.168.1.2'),
(3,'a','2012-01-04','192.168.1.1'),
(4,'c','2012-01-10','192.168.1.3');
*/
select 
B.ID,
B.Name,
B.[Time],
B.IP 
from A AS B
where B.IP in
(
    select A.IP from A
    where A.[Time] < '2012-01-04'
)
and B.[Time] between '2012-01-04' and '2012-01-10';

------解决方案--------------------
try
SQL code
SELECT ID,Name,Time,IP,
case when 
  exists(select 1 TABLE where Time<='2012-01-03 23:59:59' and IP=t.IP)
  then 'Yes'
  else 'No'
end as oldip
FROM Table
WHERE time>='2012-01-04 00:00:00' and time<='2012-01-10 23:59:59'