Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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/php中显示十大主题及其响应_Php_Mysql_Database_Subquery - Fatal编程技术网

从多个类别中获取主题,并在mysql/php中显示十大主题及其响应

从多个类别中获取主题,并在mysql/php中显示十大主题及其响应,php,mysql,database,subquery,Php,Mysql,Database,Subquery,嗨,朋友们,我正在用php列出可能与多个类别相关的主题 我的数据库模式是 topics topic_id user_id topic_name category category_id category_name topic_category (for association) tc_id topic_id category_id 主题_响应//获取结果 tr_id topic_id response ( given in a form of 5 star rating so i

嗨,朋友们,我正在用php列出可能与多个类别相关的主题

我的数据库模式是

topics

topic_id
user_id
topic_name

category 

category_id
category_name


topic_category (for association)

tc_id
topic_id
category_id
主题_响应//获取结果

tr_id
topic_id
response ( given in a form of 5 star rating so its in range of 1-5 always )
user_id
我需要做的是

1st)根据回复列出前十大主题,这将基于回复数量

我试过->

select t.* ,count(tr.response) as votes from topics t , topic_response tr where t.topic_id=tr.topic_id group by tr.topic_id order by votes LIMIT 10
不起作用

2)将向用户显示主题列表。他可以选择他想要的类别,也可以是多个类别。

比如说

如果他选择
category\u id 1,2,3,4
,则将列出该类别中列出的主题

我已经试过了

select t.* from topics t ,topic_category tc where tc.topic_id = t.topic_id and category_id IN (1,3,2,4) 
// not able to get idea on this i would prefer if i could do this in subquery since i also need to check if the user has already responded to that question .
**3) 以防我得到一个有效的查询

从php方面,我将从select multiple下拉列表中获得category_id数组 类数组(1,2,3,4)

所以我在想如何让这个查询接受category id

在mysql查询中以(1,3,2,4)类别id的形式**

**我可以直接传递这样的数组吗

category\u id在($id)
中,其中
$id
是一个数组**

我是mysql的新手,请帮帮我
感谢您的帮助:)

关于第一个问题:

您必须使用左连接并匹配(响应的主题id行)到(主题id),然后计数(响应的主题id)并按(响应的主题id)对所有内容进行分组

例如:

响应表=响应 回应

主题id

回复信息

主题表=主题 身份证

头衔

内容 问题是

选择topics.title、topics.content、COUNT(responses.topic\u id)作为topics的计数 topics.id=responses.topic\u id按计数限制分组10

关于问题2:
  • 您可以尝试使用AND(在1或2或3或4中)
  • 您可以尝试使用1到4之间的值
  • 其中(category\u id>=1&&category\u id