MySQL 查询中的左连接还是右连接?
table1: node
fields: nid, ,title,type,created
query1:
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
query2:
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
具有一一对应关系。
现在,我想获取一个 标题列表.其中数字是
22
。 降序
根据ContentSum
和created
。有办法得到这个吗?我应该使用左连接
吗?我不知道如何使两个查询变成一个?
改写一下:
表一结构{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
降序排列图块列表(decending content_id using "对于每个content_id,value=1的行数减去value=0的行数" )
2、因为table2可能少于22条记录,并且当value=1的行数减去数量时,其值相同值 = 0 的行数。当出现这种情况时。使用表 1 中的 created
字段来降序排列 tile
table1: node
fields: nid, ,title,type,created
query1:
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
query2:
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
value equals nid
value, but in table 1, the nid
maybe not has the one - to - one correspondence to the table 2. eg:
table 1 table2
nid content_id
1 1
2 3
3
but the content_id
has one - to - one correspondence to the nid
in table 1.
now, i want to get a title list
. which unmber is 22
. the descending order
is according to ContentSum
and created
. is there a way to get this? should i use left join
? i don't know how to make the two query turn into one?
a hard query to write in mysql to me?
rephrase it:
table one structure {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
table two structure {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
i want:
get 22 titles list
...
test one
test two
example one
example two
...
1, the value of nid
is equals the value of content_id
in table 2.
the title list queue order is:
1, first according to table 2 content_id
descending the tile list(decending content_id using "For each content_id, the number of rows with value=1 minus the number of rows with value=0" )
2, because table2 maybe less than 22 records and has the same value when the number of rows with value=1 minus the number of rows with value=0. when emerge this condition. using the created
field in table 1 to descending the tile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
意味着元组不必在 table2 中具有匹配元素,但将包含 table1 中的所有元素。
意味着元组不必在 table1 中具有匹配元素,但将包含 table2 中的所有元素。
Means that tuples do not have to have a matching element in table2 but all elements in table1 will be included.
Means that tuples do not have to have a matching element in table1 but all elements in table2 will be included.
通过您提供的示例,其中一些内容对我来说并不清楚。我将给出一个结合您提供的两个查询的查询。
试试这个
注意:您可以删除一些不需要的列。
其他需要了解的事项:
LEFT JOIN
还将创建一个不具有与 content_id 等效的nid
的行。这就是我添加v_api.ContentSum
的原因,以便您看到它将创建一个null
值。null
的值,则可以使用COALESCE
。如果您不想包含v_api.ContentSum
等于null
的行,则可以使用INNER JOIN
。RIGHT JOIN
根据我的理解,我不确定这是否是您针对您的问题所需要的。另请查看mysql/join。
我希望这能有所帮助。
With the example you've provided some of it is not clear to me. I'll just give a query that combining the two query you've provided.
Try this
Note: you can remove some of the columns that you don't need.
Other things to know:
LEFT JOIN
will also create a row that doesn't have an equivalentnid to content_id
. That's the reason I addedv_api.ContentSum
for you to see that it will create anull
value.COALESCE
if you want to add a value if it's returnnull
. You can useINNER JOIN
If you don't want to include the row that have av_api.ContentSum
equal tonull
.RIGHT JOIN
I'm not sure if this is what you need with regards to your question base on my understanding.Also take a look on this mysql/join.
I hope this can be a help.