【发布时间】:2011-05-21 02:54:23
【问题描述】:
table1: node
fields: nid, ,title,type,created
查询1:
mysql_query(SELECT nid,title, type,created FROM `node` where type="provider" ORDER BY created DESC LIMIT 0 , 22)
table2: votingapi_vote
fields: content_id, value=1 or value=0, value_type=option
查询2:
SELECT content_id,
SUM(CASE WHEN value=1 THEN 1
WHEN value=0 THEN -1
ELSE 0
END) AS ContentSum
FROM votingapi_vote
WHERE value_type = 'option'
GROUP BY content_id
ORDER BY ContentSum DESC
content_id值等于nid值,但在表1中,nid可能与表2没有一一对应关系。例如:
table 1 table2
nid content_id
1 1
2 3
3
但content_id 与表 1 中的nid 具有一一对应关系。
现在,我想获得title list。其中 unmber 是 22。 descending order 是根据 ContentSum 和 created。有没有办法得到这个?我应该使用left join 吗?不知道怎么把两个查询合二为一?
a hard query to write in mysql to me?
改写它:
表一结构{node}:
nid type title created
10 provider test one 1298107010
11 provider test two 1298107555
12 provider example one 1300524695
13 provider example two 1298081391
14 provider example three 1298082340
15 company example four 1298083519
16.... company example five 1298083559
表二结构{votingapi_vote}:
content_id value value_type
10 1 option
10 0 option
11 1 option
12 0 option
15 3 percent
15 2 percent
16..... 0 option
我想要:
获取22个标题列表
...
test one
test two
example one
example two
...
1,nid的值等于表2中content_id的值。
标题列表队列顺序为:
1、首先根据表2content_id对tile列表进行降序(content_id降序使用“对于每个content_id,value=1的行数减去value=0的行数”)
2,因为 table2 可能少于 22 条记录,并且当 value=1 的行数减去 value=0 的行数时具有相同的值。当出现这种情况时。使用表 1 中的created 字段对tile 进行降序处理
【问题讨论】:
-
如果您可以改写问题以使其更易于阅读并准确了解需要什么,那就太好了。
-
对不起,这个问题很难描述。即。我想得到一个标题列表。其中unmber是22。降序是根据table2 ContentSum和table1创建的。
-
我同意威尔的观点。提供两个表的 DDL、示例数据和查询的预期输出。
-
我已经更新了这个问题。期待更清楚。