Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Left Join - Fatal编程技术网

mysql |条件左连接请求

mysql |条件左连接请求,mysql,left-join,Mysql,Left Join,我有一张桌子: 投票 record_id user_id type 1 1 0 2 1 1 3 1 0 问题//键入-0 id title 1 Title1 2 Title2 3 Title3 回答//类型1 id question_id 2 2 我需要请求获取此输出数据 record_id user_id type tit

我有一张桌子:

投票

record_id   user_id   type

1           1         0
2           1         1
3           1         0
问题//键入-0

id   title
1    Title1
2    Title2 
3    Title3 
回答//类型1

id   question_id

2    2
我需要请求获取此输出数据

record_id   user_id   type   title

1           1         0      Title1
2           1         1      Title2
3           1         0      Title3
我认为这将是有条件的左连接(如果type=0,左连接带问题表;如果type=1,左连接带答案表)。 如何执行此操作?

选择 SELECT votes.record_id, votes.user_id, votes.type, question.title FROM votes LEFT JOIN question ON votes.type=0 AND question.id=votes.record_id LEFT JOIN answer ON votes.type=1 AND answer.id=votes.record_id vows.record\u id,vows.user\u id,vows.type,question.title 从…起 投票 关于投票的左连接问题。类型=0,问题。id=投票。记录\u id
左键连接投票答案。type=1和answer.id=vows。记录\u id如果我正确理解您的逻辑,这将给出您要查找的结果:

SELECT
  v.record_id,
  v.user_id,
  v.type,
  COALESCE(q1.title, q2.title) as title
FROM
  votes v LEFT JOIN question q1
  ON v.type=0 AND v.record_id=q1.id
  LEFT JOIN answer a
  ON v.type=1 AND v.record_id=a.id
  LEFT JOIN question q2
  ON a.question_id=q2.question_id

你能告诉我这个答案的更多细节吗?