热搜:NVER 

SQL语句这样的写法是否存在有关问题

2024-05-01 11:24:01
 SQL语句这样的写法是否存在有关问题

SQL语句这样的写法是否存在问题
select * from (select * from xf_art where flag=3 order by add_time desc) a,(select id userid,username from xf_usr where flag!=9 ) u where a.xfuid=u.userid order by a.add_time desc
 
以上的sql语句是我一个已离职的同事写,运行正常,但是怎么看都觉得有点别扭,SQL语句这样的写法是否存在问题呢?问题再在哪里(我现在也说不出来,所以请教各位了)

------解决方案--------------------
语句没问题,效率可能有些问题,用explain查一下。

SQL code
explain select * from (select * from xf_art where flag=3 order by add_time desc) a,(select id userid,username from xf_usr where flag!=9 ) u where a.xfuid=u.userid order by a.add_time desc

------解决方案--------------------
没有问题,从性能来看,第一个order by add_time desc可以不需要。
------解决方案--------------------
这样的sql语句没问题 是可以运行的 但是执行的效率还要进一步的修改了。

他就是 select * from 表a , 表u where a.xfuid=u.userid;
a 是个表 这个表不是数据库现有的 而是通过某种条件查询出的表。