加入mysql中的条目

时间:2012-01-16 11:18:59

标签: mysql join

这是我的问题:

query2 = "
INSERT INTO randomnusers 
  SELECT src_ip, vlan_id, video_id, area 
  from video_2 
  WHERE date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55' 
  as table1 right join (SELECT distinct src_ip, vlan_id 
                        from video_2 
                        WHERE date_pl >= '2011-11-29 00:00' 
                          AND date_pl <= '2011-12-05 23:55' 
                        order by rand() limit 50) 
  as table2 on table1.src_ip = table2.src_ip, table1.vlan_id = table2.vlan_id"

在video_2表中,每个用户(使用src_ip和vlan_id的组合来标识不同的用户)都有几个关于其下载历史记录的条目。

现在我想随机选择50个不同的用户,在名为randomnusers的新表中列出他们的所有下载历史记录。

但是这个语法在&#34;周围有一个错误,如table1&#34;谁能帮我检查一下?

非常感谢

2 个答案:

答案 0 :(得分:0)

尝试在表1中放置括号,选择如下:

(SELECT src_ip, vlan_id, video_id, area 
from video_2 
WHERE date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55') as table1

答案 1 :(得分:0)

SELECT语句的语法为SELECT t1.name, t2.salary FROM employee AS t1, info AS t2 WHERE t1.name = t2.name;

query2 = "INSERT INTO randomnusers (
    SELECT src_ip, vlan_id, video_id, area 
    from video_2  AS table1

    right join (SELECT distinct src_ip, vlan_id 
                    from video_2 
                    WHERE date_pl >= '2011-11-29 00:00' 
                      AND date_pl <= '2011-12-05 23:55' 
                    order by rand() limit 50) 
        as table2 on table1.src_ip = table2.src_ip AND table1.vlan_id = table2.vlan_id
    WHERE date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55')"