简体   繁体   English

将两个选择的SQL查询合并为一个

[英]Combining two select SQL queries into one

"id"    "type"  "parent"    "country"   "votes" "perCent"
"1"     "1"     "0"         "US"        "0"     "0"
"2"     "3"     "1"         "US"        "105"   "0"// Total
"3"     "10"    "2"         "US"        "15"    "0"
"4"     "10"    "2"         "US"        "50"    "0"
"5"     "10"    "2"         "US"        "25"    "0"
"6"     "10"    "2"         "US"        "5"     "0"
"7"     "10"    "2"         "US"        "10"    "0"

"8"     "1"     "0"         "US"        "0"     "0"
"9"     "3"     "8"         "US"        "80"    "0"// Total
"10"    "10"    "9"         "US"        "10"    "0"
"11"    "10"    "9"         "US"        "5"     "0"
"12"    "10"    "9"         "US"        "15"    "0"
"13"    "10"    "9"         "US"        "20"    "0"
"14"    "10"    "9"         "US"        "30"    "0"

In the above table likes , I have the total votes stored in a type = 3 row. 在上面的表likes ,我将总票数存储在type = 3行中。

I'm trying to update the perCent column with the percentage of votes each has got where type = 10. 我试图用类型= 10的每个投票百分比更新perCent列。

I do this right now in php like below. 我现在在php中执行以下操作。 Can the first two statements be combined into one? 前两个语句可以合并为一个吗? I lost trying with joins and inner joins etc. 我迷失了联接和内部联接等的尝试。

The way I currently do things in php is as follows: 我目前在php中做事的方式如下:

select id, votes as totalVotes from likes where type = 3 and country = 'us';

select votes from likes where parent = id and type = 10;

update votes set votes = (100*10/totalVotes) where type = 10 and parent = id;

Results I trying to achieve: 我试图达到的结果:

"id"    "type"  "parent"    "country"   "votes" "perCent"
    "1"     "1"     "0"         "US"        "0"     "0"
    "2"     "3"     "1"         "US"        "105"   "0"// Total
    "3"     "10"    "2"         "US"        "15"    "14.28" (100*15/105)
    "4"     "10"    "2"         "US"        "50"    "47.61"
    "5"     "10"    "2"         "US"        "25"    "23.80"
    "6"     "10"    "2"         "US"        "5"     "4.76"
    "7"     "10"    "2"         "US"        "10"    "9.53"

    "8"     "1"     "0"         "US"        "0"     "0"
    "9"     "3"     "8"         "US"        "80"    "0"// Total
    "10"    "10"    "9"         "US"        "10"    "12.5"
    "11"    "10"    "9"         "US"        "5"     "6.25"
    "12"    "10"    "9"         "US"        "15"    "18.75"
    "13"    "10"    "9"         "US"        "20"    "25.00"
    "14"    "10"    "9"         "US"        "30"    "37.50"

Check SQLFiddle . 检查SQLFiddle

UPDATE `votestable` v
SET v.`percent` = (SELECT
                     ROUND( ( ( v.votes * 100) / v1.votes ), 2 )
                   FROM (SELECT
                           votes,
                           id
                         FROM Votestable) AS v1
                   WHERE v1.id = v.parent)

This should do what you're asking, updating all products' percentages to reflect their part of the total votes under their heading; 这应该按照您的要求执行,更新所有产品的百分比,以反映其标题下总投票的部分;

UPDATE likes p
JOIN likes h
  ON p.parent = h.id
 AND p.type=10 AND h.type=3
 AND h.country = 'US'
SET p.percent=100*p.votes/h.votes;

An SQLfiddle to test with . 要使用进行测试的SQLfiddle

If I'm not mistaken, this looks like a left join to me. 如果我没记错的话,这就像是我的左撇子。

SELECT lp.id, lp.votes + COALESCE(SUM(lc.votes), 0) as totalVotes FROM likes lp
    LEFT JOIN likes lc ON lc.parent = lp.id AND lc.type = 10
    WHERE lp.type = 3 AND lp.country = 'us'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM