SQL 带 WHERE 子句的交叉连接不返回任何内容

SQL CROSS JOIN with WHERE CLAUSE not returning anything

我正在尝试获取查询结果:

SELECT product, exportcountry
FROM is_exported_to CROSS JOIN is_produced_in
WHERE product = cheese AND origin = 'France';

但我似乎无法得到任何 return。

创建的两个表如下:

CREATE TABLE is_produced_in(
  cheese VARCHAR(32),
  origin VARCHAR(32)
  );

  INSERT INTO is_produced_in (cheese, origin)
  VALUES 
  ('Brie', 'France'),
  ('Chrur', 'Tibet'),
  ('Halloumi', 'Greece'),
  ('Orgu', 'Turkey'),
  ('Salers', 'France'),
  ('Serrano', 'Brazil')

CREATE TABLE is_exported_to(
  product VARCHAR(32),
  exportcountry VARCHAR(32)
);

INSERT INTO is_exported_to (product, exportcountry)
VALUES
  ('Brie', 'Greece'),
  ('Halloumi', 'Brazil'),
  ('Halloumi', 'France'),
  ('Halloumi', 'Tibet'),
  ('Orgu', 'France'),
  ('Orgu', 'Tibet'),
  ('Salers', 'Greece'),
  ('Serrano', 'Tibet'),
  ('Serrano', 'Turkey')

你错过了“;”在你的初始化脚本中。

CREATE TABLE is_produced_in(
  cheese VARCHAR(32),
  origin VARCHAR(32)
  );

  INSERT INTO is_produced_in (cheese, origin)
  VALUES 
  ('Brie', 'France'),
  ('Chrur', 'Tibet'),
  ('Halloumi', 'Greece'),
  ('Orgu', 'Turkey'),
  ('Salers', 'France'),
  ('Serrano', 'Brazil');

CREATE TABLE is_exported_to(
  product VARCHAR(32),
  exportcountry VARCHAR(32)
);

INSERT INTO is_exported_to (product, exportcountry)
VALUES
  ('Brie', 'Greece'),
  ('Halloumi', 'Brazil'),
  ('Halloumi', 'France'),
  ('Halloumi', 'Tibet'),
  ('Orgu', 'France'),
  ('Orgu', 'Tibet'),
  ('Salers', 'Greece'),
  ('Serrano', 'Tibet'),
  ('Serrano', 'Turkey');

查询 sql 可以是:

SELECT product, exportcountry
FROM is_exported_to CROSS JOIN is_produced_in
ON product = cheese AND origin = 'France';

使用“ON”而不是“WHERE”

参见:https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/2

交叉连接不需要指定连接条件。 您可以使用以下查询:

SELECT * from is_exported_to 
CROSS JOIN is_produced_in 
WHERE origin='France'