日期:2014-05-18 浏览次数:20574 次
select top 1 cli_no company
------解决方案--------------------
SELECT a.list_no, a.prod_no, a.batch_no, a.inv_num, a.sell_price,b.cli_no as md_no, e.cli_no, e.cli_name, e.cli_add, e.cli_tel, e.inv_date
FROM (select top 1 cli_no company) b, dbo.inv_sub AS a LEFT OUTER JOIN
(SELECT dbo.inv_main.list_no, dbo.inv_main.cli_no, dbo.client.cli_name, dbo.client.cli_add, dbo.client.cli_tel, dbo.inv_main.inv_date
FROM dbo.inv_main LEFT OUTER JOIN
dbo.client ON dbo.inv_main.cli_no = dbo.client.cli_no) AS e ON a.list_no = e.list_no
楼主为何既 from a,b 又 from a left join b 的。
SELECT a.list_no, a.prod_no, a.batch_no, a.inv_num, a.sell_price,b.cli_no as md_no, e.cli_no, e.cli_name, e.cli_add, e.cli_tel, e.inv_date
FROM (select top 1 cli_no from company) b cross join dbo.inv_sub AS a LEFT OUTER JOIN
(SELECT dbo.inv_main.list_no, dbo.inv_main.cli_no, dbo.client.cli_name, dbo.client.cli_add, dbo.client.cli_tel, dbo.inv_main.inv_date
FROM dbo.inv_main LEFT OUTER JOIN
dbo.client ON dbo.inv_main.cli_no = dbo.client.cli_no) AS e ON a.list_no = e.list_no
试试。
------解决方案--------------------
SELECT a.list_no, a.prod_no, a.batch_no, a.inv_num, a.sell_price,b.cli_no as md_no, e.cli_no, e.cli_name, e.cli_add, e.cli_tel, e.inv_date
FROM (select top 1 cli_no company) b cross join dbo.inv_sub AS a LEFT OUTER JOIN
(SELECT dbo.inv_main.list_no, dbo.inv_main.cli_no, dbo.client.cli_name, dbo.client.cli_add, dbo.client.cli_tel, dbo.inv_main.inv_date
FROM dbo.inv_main LEFT OUTER JOIN
dbo.client ON dbo.inv_main.cli_no = dbo.client.cli_no) AS e ON a.list_no = e.list_no
------解决方案--------------------
其实原因很简单,单看这里FROM (select top 1 cli_no company) b, dbo.inv_sub AS a 就能发现,由于后面没有on子句,因此这里其实执行的是cross join,换句话说,由于你采用以前的写法select * from table1,table2如果后面没有on子句,SQL就自动按照cross join来执行了。
为了更好的说明问题,可以参考下面的代码:
CREATE TABLE t1 ( id INT ) INSERT INTO t1 SELECT 1 CREATE TABLE t2 ( id INT ) INSERT INTO t2 SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 CREATE TABLE t3 ( id INT