Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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
Php mysql中的多表查询_Php_Mysql_Sql - Fatal编程技术网

Php mysql中的多表查询

Php mysql中的多表查询,php,mysql,sql,Php,Mysql,Sql,我正在尝试查询4个表(当我查询前3个表时效果很好),我的表结构如下所示 1表-类别 catid catname 1 AAA 2 BBB 2表-问题 quid qtype 1 data 2 3 data 表3-答案 ansid quid catid userid answer 1 1 1 1 test1 2 2 1 1

我正在尝试查询4个表(当我查询前3个表时效果很好),我的表结构如下所示

1表-类别

catid   catname
1       AAA
2       BBB
2表-问题

quid   qtype
1      data
2      
3      data
表3-答案

ansid   quid   catid   userid   answer
1       1      1       1        test1
2       2      1       1        test2
3       3      2       1        test3
表4-关注事项

concern_id   catid   userid   ansid
1            1       1        1
2            1       4        3
现在我的查询是(有3个表)

它给了我(这很好)

现在我想在查询中包括第4个表,结果应该如下

concern_id   catname   quid   catid   ansid   answer
1             AAA       1      1       1       test1
null          BBB       3      2       3       test3
在这里,我被这个查询困住了。

试试这个:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM 
questions 
INNER JOIN answers  on (questions.quid = answers.quid )
INNER JOIN category cat on (category.catid = answers.catid)
LEFT JOIN concerns con on (concerns.userId = answers.userId and concerns.catid = answers.catid  AND concerns.ansid = answers.ansid )
where  questions.qtype = 'data' 

and answers.userid = 1
试试这个:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM 
questions 
INNER JOIN answers  on (questions.quid = answers.quid )
INNER JOIN category cat on (category.catid = answers.catid)
LEFT JOIN concerns con on (concerns.userId = answers.userId and concerns.catid = answers.catid  AND concerns.ansid = answers.ansid )
where  questions.qtype = 'data' 

and answers.userid = 1
试试这个:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM 
questions 
INNER JOIN answers  on (questions.quid = answers.quid )
INNER JOIN category cat on (category.catid = answers.catid)
LEFT JOIN concerns con on (concerns.userId = answers.userId and concerns.catid = answers.catid  AND concerns.ansid = answers.ansid )
where  questions.qtype = 'data' 

and answers.userid = 1
试试这个:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM 
questions 
INNER JOIN answers  on (questions.quid = answers.quid )
INNER JOIN category cat on (category.catid = answers.catid)
LEFT JOIN concerns con on (concerns.userId = answers.userId and concerns.catid = answers.catid  AND concerns.ansid = answers.ansid )
where  questions.qtype = 'data' 

and answers.userid = 1
您需要使用联接:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM category

join answers 
on category.catid = answers.catid 

join questions
on questions.quid = answers.quid

join concern
on concerns.userId = answers.userId and concerns.catid = answers.catid  AND      concerns.ansid = answers.ansid

where  questions.qtype = 'data' 
and answers.userid = 1
您需要使用联接:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM category

join answers 
on category.catid = answers.catid 

join questions
on questions.quid = answers.quid

join concern
on concerns.userId = answers.userId and concerns.catid = answers.catid  AND      concerns.ansid = answers.ansid

where  questions.qtype = 'data' 
and answers.userid = 1
您需要使用联接:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM category

join answers 
on category.catid = answers.catid 

join questions
on questions.quid = answers.quid

join concern
on concerns.userId = answers.userId and concerns.catid = answers.catid  AND      concerns.ansid = answers.ansid

where  questions.qtype = 'data' 
and answers.userid = 1
您需要使用联接:

SELECT category.catname,questions.quid,answers.catid,answers.ansid,answers.answer

FROM category

join answers 
on category.catid = answers.catid 

join questions
on questions.quid = answers.quid

join concern
on concerns.userId = answers.userId and concerns.catid = answers.catid  AND      concerns.ansid = answers.ansid

where  questions.qtype = 'data' 
and answers.userid = 1

没有必要使用联接。MySQL将把加入的版本编译成与此版本相同的结果

我认为你应该用ansid把它们结合起来

SELECT concern.concern_id, category.catname, questions.quid, answers.catid, answers.ansid, answers.answer

FROM questions, answers, category, concerns

where questions.qtype = 'data' 

and concerns.ansid = answers.ansid

and questions.quid = answers.quid 

and category.catid = answers.catid 

and answers.userid = 1

没有必要使用联接。MySQL将把加入的版本编译成与此版本相同的结果

我认为你应该用ansid把它们结合起来

SELECT concern.concern_id, category.catname, questions.quid, answers.catid, answers.ansid, answers.answer

FROM questions, answers, category, concerns

where questions.qtype = 'data' 

and concerns.ansid = answers.ansid

and questions.quid = answers.quid 

and category.catid = answers.catid 

and answers.userid = 1

没有必要使用联接。MySQL将把加入的版本编译成与此版本相同的结果

我认为你应该用ansid把它们结合起来

SELECT concern.concern_id, category.catname, questions.quid, answers.catid, answers.ansid, answers.answer

FROM questions, answers, category, concerns

where questions.qtype = 'data' 

and concerns.ansid = answers.ansid

and questions.quid = answers.quid 

and category.catid = answers.catid 

and answers.userid = 1

没有必要使用联接。MySQL将把加入的版本编译成与此版本相同的结果

我认为你应该用ansid把它们结合起来

SELECT concern.concern_id, category.catname, questions.quid, answers.catid, answers.ansid, answers.answer

FROM questions, answers, category, concerns

where questions.qtype = 'data' 

and concerns.ansid = answers.ansid

and questions.quid = answers.quid 

and category.catid = answers.catid 

and answers.userid = 1


尝试使用Bit Middle来使用left join,因为有4个表,所以不确定哪一个要保留为left table,哪一个要保留为Right尝试使用Bit Middle来使用left join,因为有4个表,所以不确定哪一个要保留为left table,哪一个要保留为Right尝试使用Bit Middle来使用left join,因为有4个表,所以不确定要保留哪一个保留为左表,哪一个保留为右尝试使用Bit Middle使用左联接,因为有4个表,所以不确定哪一个保留为左表,哪一个保留为右表忘记包含最后一个表:)忘记包含最后一个表:)忘记包含最后一个表:)忘记包含最后一个表:)描述解决方案。描述解决方案的问题。描述解决方案的问题。描述解决方案的问题。谢谢Sashi…我对查询做了一些修改,工作得很有魅力。我在前3个表上做左连接…我真是个白痴,我还是…:D谢谢你的帮助t帮了大忙:)谢谢Sashi…我对查询做了一些修改,工作起来很有魅力。我在前三张表上做了左连接…我真是个白痴,我仍然很好…:D谢谢你的帮助t帮了大忙:)谢谢Sashi…我对查询做了一些修改,工作起来很有魅力。我在前三张表上做了左连接…我真是个白痴,我仍然很好…:D谢谢你的帮助t帮了大忙:)谢谢Sashi…我对查询做了一些修改,工作起来很有魅力。我在前三张表上做了左连接…我真是个白痴,我仍然很好…:D谢谢你的帮助伟大的t帮助:)