Select 来自两个没有 CommonElement 的表

Select from two tables without CommonElement

我想从两个没有任何共同元素的表中制作 select 我正在尝试这段代码,但在“,”附近出现了不正确的语法。

select top 1 inventory.name, customer.name
from inventory
where inventor.name='test' ,  customer where customer.name='test2'

它导致两个表的笛卡尔积。 (交叉连接)。像这样尝试,

SELECT TOP 1 i.NAME
    ,c.NAME
FROM inventory i
cross join customer c
WHERE i.NAME = 'test'
    AND c.NAME = 'test2'

where 子句不使用 , 作为条件。您想要 andor 来连接条件。您还需要 join.

所以,我想你打算:

select top 1 i.name, c.name
from inventory i cross join
     customer c 
where i.name = 'test' and c.name = 'test2';

通常情况下,join 会有一个 join 条件,但您的问题并没有暗示。这是预期的:

select top 1 i.name, c.name
from inventory i cross join
     customer c 
     on i.customer_id = c.customer_id
where i.name = 'test' and c.name = 'test2';

但是,话又说回来,您限制了 where 子句中的 name 列,所以为什么不这样做:

select 'test', 'test2';

完全不用表格?

您可以简单地为所需的内容编写如下所示的查询output.Assuming您只需要前 1 列..

SELECT (SELECT top 1 i.name
        FROM inventory i 
        WHERE i.name = 'test') InventoryName,
       (SELECT TOP 1 c.name
        FROM customer c 
        WHERE c.name = 'test2') Customername