I have the following MySQL query:
SET #sum1 = 0;
SELECT (#sum1 := #sum1 + 1),date_clicked
FROM `documents_clicks_or_downloads` dclick
JOIN `documents_repository` drep ON dclick.documentID = drep.ID
WHERE accountID = 2304
ORDER BY dclick.ID ASC
Here are the results I get:
"(#sum1 := #sum1 + 1)" "date_clicked"
"1" "2014-04-04"
"2" "2014-04-04"
"3" "2014-04-04"
"4" "2014-04-04"
"5" "2014-04-04"
"6" "2014-04-04"
"7" "2014-04-04"
"8" "2014-04-04"
"9" "2014-04-04"
"10" "2014-04-04"
"11" "2014-04-04"
"12" "2014-04-04"
"13" "2014-04-04"
"14" "2014-04-04"
"15" "2014-04-04"
"16" "2014-04-04"
"17" "2014-04-04"
"18" "2014-04-04"
"19" "2014-04-04"
"20" "2014-04-04"
"21" "2014-04-04"
"22" "2014-04-04"
"23" "2014-04-04"
"24" "2014-04-04"
"33" "2014-04-04"
"34" "2014-04-04"
"35" "2014-04-04"
"36" "2014-04-04"
"37" "2014-04-04"
"38" "2014-04-04"
"39" "2014-04-04"
"40" "2014-04-04"
"41" "2014-04-04"
"42" "2014-04-04"
"43" "2014-04-04"
"44" "2014-04-04"
"45" "2014-04-04"
"46" "2014-04-04"
"47" "2014-04-04"
"48" "2014-04-04"
"49" "2014-04-04"
"50" "2014-04-04"
"51" "2014-04-04"
"52" "2014-04-04"
"53" "2014-04-04"
"54" "2014-04-04"
"25" "2014-04-04"
"26" "2014-04-04"
"27" "2014-04-04"
"55" "2014-04-04"
"56" "2014-04-04"
"57" "2014-04-04"
"28" "2014-04-04"
"29" "2014-04-05"
"58" "2014-04-05"
"30" "2014-04-06"
"31" "2014-04-06"
"59" "2014-04-06"
"61" "2014-04-07"
"62" "2014-04-07"
"63" "2014-04-07"
"64" "2014-04-07"
"65" "2014-04-07"
"66" "2014-04-07"
"67" "2014-04-07"
"68" "2014-04-07"
"69" "2014-04-07"
"70" "2014-04-07"
"71" "2014-04-07"
"72" "2014-04-07"
"73" "2014-04-07"
"74" "2014-04-07"
"75" "2014-04-07"
"76" "2014-04-07"
"77" "2014-04-07"
"87" "2014-04-07"
"88" "2014-04-07"
"89" "2014-04-07"
"90" "2014-04-07"
"91" "2014-04-07"
"92" "2014-04-07"
"93" "2014-04-07"
"94" "2014-04-07"
"95" "2014-04-07"
"96" "2014-04-07"
"97" "2014-04-07"
"98" "2014-04-07"
"99" "2014-04-07"
"100" "2014-04-07"
"101" "2014-04-07"
"102" "2014-04-07"
"103" "2014-04-07"
"104" "2014-04-07"
"105" "2014-04-07"
"106" "2014-04-07"
"107" "2014-04-07"
"78" "2014-04-07"
"79" "2014-04-07"
"108" "2014-04-07"
"109" "2014-04-07"
So the question is, why am I not getting a correct numeric increase on the sum column? It seems to jump all over the place and I can't tell why. Thanks.
The problem is that the select happens before the sorting.
Try doing it as a subquery instead:
SET #sum1 = 0;
SELECT (#sum1 := #sum1 + 1), t.*
FROM (
SELECT
date_clicked
FROM `documents_clicks_or_downloads` dclick
JOIN `documents_repository` drep ON dclick.documentID = drep.ID
WHERE accountID = 2304
ORDER BY dclick.ID ASC
) t
Related
I'm trying to update a row in table amga with just one row from table amgb joined by their itemTempId.
My problem is that, there may be upto 6 rows in table amgb for that itemTempIdand I need to use only one from it for the update.
I'm familiar with doing updates with joins, but when I added a Limit (so as to get just one row) I get the error message Incorrect usage of update and limit. I read that this is not possible, but would there be another way to do this?
amga
"id" "itemId" "itemTempId" "itemImageName" "itemName" "itemCountry" "userId"
"1" "US1" "T001" \N "Samsung Galaxy Note 5" "US" "1"
"2" "CA2" "T002" \N "Samsung Galaxy Note 6" "CA" "2"
"3" "UK3" "T003" \N "Samsung Galaxy Note 7" "UK" "3"
amgb
"id" "itemId" "itemTempId" "itemImageName" "userId"
"1" "US1" "T001" "front.jpg" "1"
"2" "US1" "T001" "side-left.jpg" "1"
"3" "US1" "T001" "side-right.jpg" "1"
"4" "US1" "T001" "back.jpg" "1"
"5" "CA2" "T002" "front.jpg" "2"
"6" "CA2" "T002" "side-left.jpg" "2"
"7" "CA2" "T002" "side-right.jpg" "2"
"8" "CA2" "T002" "back.jpg" "2"
"9" "UK3" "T003" "front.jpg" "3"
Sql I used
update amga a inner join amgb b on a.itemTempId = b.itemTempId
set a.itemImageName = b.itemImageName where a.itemTempId = 'T001' limit 1;
Expected results: Table amga after update
"id" "itemId" "itemTempId" "itemImageName" "itemName" "itemCountry" "userId"
"1" "US1" "T001" front.jpg "Samsung Galaxy Note 5" "US" "1"
"2" "CA2" "T002" \N "Samsung Galaxy Note 6" "CA" "2"
"3" "UK3" "T003" \N "Samsung Galaxy Note 7" "UK" "3"
Note: itemTempId is updated with front.jpg, which is the first row for itemTempId = T001 in amgb
Any help appreciated.
Update
I noticed it works if I remove the limit, and that it updates too. But is it the right way to do it? What does MySql do with the other rows in the select?
update amga a inner join amgb b on a.itemTempId = b.itemTempId
set a.itemImageName = b.itemImageName where a.itemTempId = 'T001';
Maybe you can use a subquery:
UPDATE amga a
SET a.itemImageName =
(SELECT b.itemImageName
FROM amgb b
WHERE b.itemTempId = 'T001'
ORDER BY b.id LIMIT 1)
WHERE a.itemTempId = 'T001'
This question already has answers here:
Rank function in MySQL
(13 answers)
Closed 8 years ago.
How can I calculate the rank for all "type 10" rows in the data below using just sql?
The sql will go into a stored procedure, with no other scripting involved.
The parent holds the total of all rows in column total, and the total votes in votes.
I update the perCent col using this, so this should give you an idea. Maybe calculate ranks along with this?
All rows are linked by parent -> child relationship.
All is based on total votes and total candidates. Candidates are type 10
UPDATE likesd p
JOIN likesd h
ON p.parent = h.id
AND p.country = h.country
SET p.percent = TRUNCATE(100*p.votes/h.votes,2);
Raw Data
"id" "type" "parent" "country" "votes" "perCent" "total" "rank"
"24" "1" "1" "US" "30" "0" "" "0"
"25" "3" "24" "US" "30" "0" "3" "0"
"26" "10" "25" "US" "15" "50.00" "" "0"
"27" "10" "25" "US" "5" "16.66" "" "0"
"28" "10" "25" "US" "10" "33.33" "" "0"
Desired results
"id" "type" "parent" "country" "votes" "perCent" "total" "rank"
"24" "1" "1" "US" "30" "0" "" "0"
"25" "3" "24" "US" "30" "0" "3" "0"
"26" "10" "25" "US" "15" "50.00" "" "1" // Rank 1. Has 15 votes out of 30 (see parent row above)
"27" "10" "25" "US" "5" "16.66" "" "3" // And so on.
"28" "10" "25" "US" "10" "33.33" "" "2"
SELECT id,type,parent,country,votes,perCent,total, FIND_IN_SET( votes, (
SELECT GROUP_CONCAT( votes
ORDER BY votes DESC )
FROM table WHERE type=10 )
) AS rank
FROM table
SQL Fiddle
SQL Fiddle
UPDATE scores SET rank= (FIND_IN_SET(votes, (
SELECT * FROM(SELECT GROUP_CONCAT( votes
ORDER BY votes DESC )
FROM scores WHERE type=10)x ) ) )
I thought I had this, but it's clear I don't. From the table below, I'm trying to display users who have made the most positive contributions (articles) on top, followed by the ones who didn't. The table is simple, artc_id is the article Id, artc_status is the status which shows if an article was approved or not. 0 is approved, 1 is not, then comes the user who wrote the article.
The results I'm trying to achieve are as follows:
Total Contributions Positive Contributing User
4 4 2
3 2 1
1 1 4
3 0 3
Table
"id" "artc_id" "artc_status" "artc_user" "artc_country"
"1" "1" "0" "1" "US"
"2" "2" "0" "1" "US"
"3" "3" "1" "1" "US"
"4" "4" "0" "2" "US"
"5" "5" "0" "2" "US"
"6" "6" "0" "2" "US"
"7" "7" "0" "2" "US"
"8" "8" "1" "3" "US"
"9" "9" "1" "3" "US"
"10" "10" "1" "3" "US"
"11" "11" "0" "4" "US"
The Sql I came up with
select count(artc_status) as stats , artc_user from contributions where artc_status = 0 group by artc_user order by stats desc;
I'm not having much luck getting results like I posted above. Can you please assist? This is completely beyond me.
select
count(artc_status) as stats ,
count(case when artc_status=1 then 1 end) Positive,
artc_user[Contributing User]
from
contributions
group by
artc_user
order by stats desc;
I think you just need conditional aggregation to get the two summary columns:
select count(*) as TotalContributions, count(artc_status = 0) as PositiveContributions, artc_user
from contributions
group by artc_user
order by PositiveContributions desc;
"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.
I'm trying to update the perCent column with the percentage of votes each has got where type = 10.
I do this right now in php like below. 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:
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"
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.
Check 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)
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'
I have a column, it's time is 'DATETIME'. I want to output it in the order of timeline, but when I use ORDER BY create_time DESC, '2011-09-10 16:16:04' is behind '2011-07-16 03:20:01'.
Seems it is treating datatime column as string.
How can I handle this without changing the column type?
UPDATE:
This is part of my query:
SELECT 'bookcomment' AS type
,b.book_id
,b.name
,c.book_id
,c.author_id
,c.content
,c.create_time as create_time
,u.id
,u.name
FROM tbl_book AS b,
tbl_book_comment AS c,
tbl_user AS u
WHERE u.id=c.author_id in (1,2) AND b.book_id=c.book_id
UNION ALL
SELECT 'bookreview' AS type
,b.book_id
,b.name
,re.book_id
,re.author_id
,re.content
,re.create_time as create_time
,u.id
,u.name
FROM tbl_book AS b,
tbl_book_review AS re,
tbl_user AS u
WHERE u.id=re.author_id in (1,2) AND b.book_id=re.book_id
UNION ALL
...
ORDER BY create_time DESC
And part of the output:
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "Whatever" ["author_id"]=> string(4) "test" ["content"]=> string(19) "2011-07-16 03:20:01" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(13) "sdfwefwaefwea" ["content"]=> string(19) "2011-05-11 03:33:33" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test0" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test1" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test2" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test3" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
array(7) { ["type"]=> string(16) "new post comment" ["book_id"]=> string(1) "1" ["name"]=> string(9) "whatever" ["author_id"]=> string(5) "test4" ["content"]=> string(19) "2011-09-10 16:16:04" ["create_time"]=> string(1) "3" ["id"]=> string(1) "1" }
You appear to have the data in the wrong columns. In your output sample create_time is 3 for all of them, so the order is undefined. Double-check data is in the right columns?
try using
ORDER BY CONVERT (create_time, DATETIME) DESC
BUT beware that this costs some performance... changing the column type is IMHO the better option !