从同一个表中加入三个查询

时间:2021-11-08 01:08:35

I have to join 3 queries from same table. I have tried this code:

我必须从同一个表加入3个查询。我试过这段代码:

SELECT t1.`board`
      ,t1.`price`
      ,t2.`price`
      ,t3.`price`
  from boardtype t1
  RIGHT JOIN(SELECT `price`
      from boardtype
      WHERE `acnonac`='ac' AND `roomtype`='single')
  t2 on t1.`board` = t2.`board`
  RIGHT JOIN(
      SELECT 
      `price`
      from boardtype 
      WHERE `acnonac`='ac' AND `roomtype`='double'
  ) t3 on t1.`board` = t3.`board`
WHERE `acnonac`='ac' AND `roomtype`='triple'
ORDER by id

Simply say I'm trying to join these three queries:

简单地说我正在尝试加入这三个查询:

SELECT `board`,`price` FROM boardtype WHERE `acnonac`='ac' AND `roomtype`='single'
SELECT `board`,`price` FROM boardtype WHERE `acnonac`='ac' AND `roomtype`='double'
SELECT `board`,`price` FROM boardtype WHERE `acnonac`='ac' AND `roomtype`='triple'

I don't know where I made mistake.

我不知道我犯了什么错误。

2 个解决方案

#1


2  

You have no board in your subselects, and yet you write the ON clauses using them.

您的子选择中没有板,但是您使用它们编写ON子句。

Add the board to the subselects:

将板添加到子选择中:

SELECT t1.`board`
      ,t1.`price`
      ,t2.`price`
      ,t3.`price`
  from boardtype t1
  RIGHT JOIN
      (SELECT `price`, `board`
      from boardtype
      WHERE `acnonac`='ac' AND `roomtype`='single') t2 on t1.`board` = t2.`board`
  RIGHT JOIN(
      SELECT `price`, `board`
      from boardtype
      WHERE `acnonac`='ac' AND `roomtype`='double') t3 on t1.`board` = t3.`board`
WHERE `acnonac`='ac' AND `roomtype`='triple'
ORDER by id

#2


1  

Maybe it was more efficient

也许它更有效率

SELECT `boardtype`.`board`
      ,`boardtype`.`price`
  from `boardtype` WHERE `boardtype`.`acnonac`='ac' AND `boardtype`.`roomtype` IN ('single', 'double', 'triple')

Thanks

谢谢

#1


2  

You have no board in your subselects, and yet you write the ON clauses using them.

您的子选择中没有板,但是您使用它们编写ON子句。

Add the board to the subselects:

将板添加到子选择中:

SELECT t1.`board`
      ,t1.`price`
      ,t2.`price`
      ,t3.`price`
  from boardtype t1
  RIGHT JOIN
      (SELECT `price`, `board`
      from boardtype
      WHERE `acnonac`='ac' AND `roomtype`='single') t2 on t1.`board` = t2.`board`
  RIGHT JOIN(
      SELECT `price`, `board`
      from boardtype
      WHERE `acnonac`='ac' AND `roomtype`='double') t3 on t1.`board` = t3.`board`
WHERE `acnonac`='ac' AND `roomtype`='triple'
ORDER by id

#2


1  

Maybe it was more efficient

也许它更有效率

SELECT `boardtype`.`board`
      ,`boardtype`.`price`
  from `boardtype` WHERE `boardtype`.`acnonac`='ac' AND `boardtype`.`roomtype` IN ('single', 'double', 'triple')

Thanks

谢谢