0

我有两张桌子

表A

  imei_no | user_name | date_of_allocation | date_of_deallocation
     123  |    a      |      1-4-2013      |         10-4-2013         
     456  |    a      |     10-4-2013      |         25-4-2013
     789  |    a      |     25-4-2013      |         30-4-2013
     123  |    b      |     25-4-2013      |         27-4-2013

表 B

  imei | lat | long |     gpsdate
   123 | 23  |  43  |     2-4-2013
   123 | 23  |  43  |     2-4-2013
   456 | 23  |  43  |     3-4-2013
   123 | 23  |  43  |     3-4-2013
   789 | 23  |  43  |     24-4-2013
   123 | 23  |  43  |     24-4-2013
   456 | 23  |  43  |     28-4-2013
   123 | 23  |  43  |     28-4-2013

我希望特定用户的 imei 从日期 5-4-2013 到 25-4-2013 我这样做:

select imei from A where user_name='a' and date_of_allocation>='5-4-2013' and date_of_deallocation<='25-4-2013';

我想从表 B 中获取该用户的数据。我该怎么做。

select Imei of user a from some date to some date,then data of that imei for that user from table B
4

3 回答 3

0

就像是

select * from B where imei in 
    (select imei_no from A where username='a' and ...)

或者您可以使用连接:

select * from A left join B on (A.imei_no = B.imei) where A.username='a' and ...
于 2013-08-10T12:14:13.417 回答
0

您可以在表单中使用子查询

SELECT * FROM B WHERE imei IN ( SELECT imei_no FROM A WHERE .... )

您也可以使用加入

SELECT * FROM A LEFT JOIN B ON (A.imei_no = B.imei) WHERE ...

http://dev.mysql.com/doc/refman/5.0/en/join.html

有许多可能的选项,具体取决于您的特定应用程序、表格大小等。

于 2013-08-10T12:10:27.403 回答
0

试试喜欢

SELECT A.*,B.lat,B.long,B.gpsdate 
FROM A
JOIN B 
    ON A.imei_no = B.imei
WHERE user_name='a' AND
      date_of_allocation >= '5-4-2013' AND
      date_of_deallocation <= '25-4-2013'
于 2013-08-10T12:10:29.137 回答