Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何重写此查询以使其正常工作,mysql从_Mysql_Sql_Subquery_Left Join - Fatal编程技术网

如何重写此查询以使其正常工作,mysql从

如何重写此查询以使其正常工作,mysql从,mysql,sql,subquery,left-join,Mysql,Sql,Subquery,Left Join,主要问题在于这个问题: 重要的想法是设法解决这个问题: 这是一个简单的查询问题示例 CREATE TABLE a ( id int ); CREATE TABLE b ( id int ); CREATE TABLE c ( id int ); SELECT id , (SELECT MIN(b.id) FROM b LEFT JOIN c ON b.id = c.id AND c.id = a.id WHERE b.id = a.id ) AS someth

主要问题在于这个问题:

重要的想法是设法解决这个问题:

这是一个简单的查询问题示例

CREATE TABLE a ( id int );
CREATE TABLE b ( id int );
CREATE TABLE c ( id int );

SELECT id
, (SELECT MIN(b.id)
   FROM b LEFT JOIN c ON b.id = c.id 
       AND c.id = a.id
   WHERE b.id = a.id
  ) AS something
FROM a
;
The column 'a.id' in on clause is unknown (within LEFT JOIN)
现在,尝试解决我想尝试使用此查询的问题:

select drf2.opcion 
from respuestas r2, opciones o2
left join detalle_respuesta_fija drf2 on o2.id_opcion = drf2.opcion and r2.id_respuesta
where o2.pregunta = 857
and r2.id_respuesta = 190968

 #1054 - La columna 'r2.id_respuesta' en on clause es desconocida
我需要查询返回以下示例:

+--------
|Result
+--------
| id
| id
| NULL
| id
| NULL
+--------
.

切勿在FROM子句中使用逗号。始终使用正确、明确、标准的联接语法

在您的情况下,这将是:

select drf2.opcion 
from respuestas r2 join
     opciones o2
     on r2.id_respuesta = o2.? left join  -- what column is used for the `JOIN`???
     detalle_respuesta_fija drf2
     on o2.id_opcion = drf2.opcion
where o2.pregunta = 857 and r2.id_respuesta = 190968;
如果前两个表之间没有连接键,只需使用交叉连接:


您应该通过id连接a表和分组,而不是子查询

 SELECT a.id MIN(b.id)
 FROM b 
 INNER JOIN a ON a.id = b.id 
 LEFT JOIN c ON b.id = c.id 
     AND c.id = a.id
 GROUP BY a.id

MySQL的8.0.14+增加了对SQL标准1999+关键字的支持,该关键字允许编写相关联。。或者更好地说,deliverd表a.k.a子查询前缀为LATERAL,可以在delivered表的左侧使用别名,这意味着。。。横向选择。。。。b、 id=a.id变得有效..您的简单示例显示了MySQL中的一个bug,但没有解释您想要什么,因为它没有意义。ON子句中的条件c.id=a.id是多余的,因为WHERE子句使a.id等于b.id;示例输入,具有预期和实际输出,包括逐字错误消息;标签和版本;清晰的说明和解释。这包括您能给出的最少代码,即您显示为OK的代码,由您显示为not OK的代码扩展。调试基础。PS您有一个语法错误。阅读语法和手册。显示组成子表达式是正确的。明确你的问题是关于那个错误的&稍后在一篇新文章中重新询问你的总体目标。请转述或引用其他页面。使您的文章独立。逗号表示交叉联接,但优先级低于关键字联接。因此,左连接首先完成&逗号的左表在这里是未知的。所以不要把逗号和关键字连接混在一起。这是一个常见问题。在考虑发帖之前,请始终用谷歌搜索任何错误消息,以及你的问题/问题/目标的许多清晰、简洁和准确的措辞,有没有你的特定字符串、名称和行号,有没有“site:stackoverflow.com”,并阅读许多点击和答案。如果你发布一个问题,用一句话作为标题。请参见文本上方的投票箭头(&S)。
 SELECT a.id MIN(b.id)
 FROM b 
 INNER JOIN a ON a.id = b.id 
 LEFT JOIN c ON b.id = c.id 
     AND c.id = a.id
 GROUP BY a.id