楼上的sql意思是说取出table1 table2 table3 内10条数据嘛?
select * from table1 by hit desc limit 10
union
select * from table2 by hit desc limit 10
union
select * from table3 by hit desc limit 10就可以了 不用再在外面加select* from ()了吧没研究过union和join的快慢 个人感觉join快吧。
select * from table1 by hit desc limit 10
union
select * from table2 by hit desc limit 10
union
select * from table3 by hit desc limit 10就可以了 不用再在外面加select* from ()了吧没研究过union和join的快慢 个人感觉join快吧。
2.最好用join
3.SQL的速度跟索引有很大关系
并且限制取出的条数.
比较一下
可以不用子查询的(select * from table1 order by hit desc limit 10)
union
(select * from table2 order by hit desc limit 10)
union
(select * from table2 order by hit desc limit 10)
order by hit desc limit 10;这句就可以完成。另外,对于相同结构,同类数据的多个表一起查询时,可以使用merge表
create table table_merge ( id int(10) unsigned not null auto_increment, title varchar(255) not null default '', hit int(10) unsigned not null, primary key (id),key hit(hit)) ENGINE=merge UNION=(table1,table2,table3) INSERT_METHOD=FIRST;
然后select * from table_merge order by hit desc limit 10;