MySQL 查询中的左连接还是右连接?

发布于 2024-11-08 23:42:46 字数 2577 浏览 0 评论 0原文

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降序根据ContentSumcreated。有办法得到这个吗?我应该使用左连接吗?我不知道如何使两个查询变成一个?

在mysql中向我写入一个硬查询?

改写一下

表一结构{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-11-15 23:42:46
<table1> left join <table2>

意味着元组不必在 table2 中具有匹配元素,但将包含 table1 中的所有元素。

<table1> right join <table2>

意味着元组不必在 table1 中具有匹配元素,但将包含 table2 中的所有元素。

<table1> left join <table2>

Means that tuples do not have to have a matching element in table2 but all elements in table1 will be included.

<table1> right join <table2>

Means that tuples do not have to have a matching element in table1 but all elements in table2 will be included.

拍不死你 2024-11-15 23:42:46

通过您提供的示例,其中一些内容对我来说并不清楚。我将给出一个结合您提供的两个查询的查询。

试试这个

SELECT nid,title, type,created, v_api.ContentSum
  FROM `node` n
  LEFT JOIN ( 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) v_api 
    ON n.nid = v_api.content_id
 where type="provider"
ORDER BY v_api.ContentSum DESC, created DESC LIMIT 0 , 22;

注意:您可以删除一些不需要的列。

其他需要了解的事项:

  • 使用 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

SELECT nid,title, type,created, v_api.ContentSum
  FROM `node` n
  LEFT JOIN ( 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) v_api 
    ON n.nid = v_api.content_id
 where type="provider"
ORDER BY v_api.ContentSum DESC, created DESC LIMIT 0 , 22;

Note: you can remove some of the columns that you don't need.

Other things to know:

  • Using LEFT JOIN will also create a row that doesn't have an equivalent nid to content_id. That's the reason I added v_api.ContentSum for you to see that it will create a null value.
  • You can use COALESCE if you want to add a value if it's return null. You can use INNER JOIN If you don't want to include the row that have a v_api.ContentSum equal to null.
  • About 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文