I have the following MYSQL:
SELECT DISTINCT(`user_id`), `type`, `link`, `add_text`
FROM `activity` WHERE `id` != 0 AND `type` !=6 AND (`type` = 4 OR `type` = 5)
ORDER BY `id` DESC LIMIT 4
I only want unique user_ids but I am getting repetitions in my sql results. What is the correct modification to make to this sql so that no repetitions of user_ids
If you don't care about the types, links or add text for each user you could use min/max and group by.
SELECT user_id, max(type), max(link), max(add_text)
FROM activity
WHERE id != 0 A
AND type in (4,5)
Group by User_Id
ORDER BY id DESC LIMIT 4
If you do care you could group_concat the type, link and add text
SELECT user_id, Group_Concat(type), Group_Concat(link), Group_Concat(add_text)
FROM activity
WHERE id != 0 A
AND type in (4,5)
Group by User_Id
ORDER BY id DESC LIMIT 4
edied group by shoudl only be on user_ID
SELECT GROUP_CONCAT(DISTINCT `user_id`) user_ids, `type`, `link`, `add_text`
FROM `activity`
WHERE `id` != 0 AND `type` IN (4,5)
GROUP BY `type`, `link`, `add_text` DESC LIMIT 4;
Related
I have the following MySQL query
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
This will grab me a random category where there is at least 5 questions in that category.
Now I want to grab all the rows for that category. So how can I do a second SELECT WHERE category is equal to the category returned from the previous query?
I tried the following but I believe the RAND() is causing it to crash/timeout.
SELECT *
FROM `jeopardy_questions`
WHERE `category` = (
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
)
You can use the above query as a subquery. Something like this:
SELECT *
FROM `jeopardy_questions`
WHERE `category` = (
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
)
This query
SELECT *
FROM `rounds`
WHERE `user` = 18956 AND `handicapDifferential` IS NOT NULL
ORDER BY `handicapDifferential` DESC, `date` DESC
LIMIT 0, 2
Gives expected result of two rows with -1.4 and -5.1 in handicapDifferential. The field datatype is FLOAT.
Using AVG() on the same result with this query
SELECT AVG(`handicapDifferential`) as `avg`
FROM `rounds`
WHERE `user` = 18956 AND `handicapDifferential` IS NOT NULL
ORDER BY `handicapDifferential` DESC, `date` DESC
LIMIT 0, 2
Gives -9.485714214188713 where I expect -3.25.
Here is a fiddle demonstrating the result and the issue: http://sqlfiddle.com/#!9/32acd/3
What am I doing incorrectly here?
Alter your query to
SELECT
AVG(items.avg)
FROM
(SELECT
`handicapDifferential` AS `avg`
FROM
`rounds`
WHERE `user` = 18956
AND `handicapDifferential` IS NOT NULL
ORDER BY `handicapDifferential` DESC,
`date` DESC
LIMIT 0, 2) items
refer link
Explanation : AVG is a an aggregate keyword in MySQL and you should have given a result set as the input as per your requirement. But u called AVG as a member in your query.
I have a mysql table with that is called transactions and have the following fields: user (varchar), amount (float).
I want to make a group by like this
select `user`
, sum(`amount`) as s
from (
select *
from `transactions`
order by `amount` desc
) t group by `user`, s
but I want to limit the sum only on the top 10 amounts.
Is it possible to do that with plain sql?
Yes, use limit and don't group by sum:
select `user`
, sum(`amount`) as s
from (
select *
from `transactions`
order by `amount` desc
limit 10
) t group by `user`
Hi i'm trying to get some random results ordered by the location ASC.
This is my query:
SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY RAND(), `location` ASC
LIMIT 8
the problem is that it gets randomly but doesn't orders then by "location" ASC, also if i do this:
SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY `location` ASC,RAND()
LIMIT 8
it doesn't gets randomly.
How can i get both togheter RAND() and ORDER BY location ASC ?
You need nested statements/queries:
SELECT *
FROM (
SELECT `location`, `route`
FROM `foo`
WHERE `location` != ''
ORDER BY RAND()
LIMIT 8) AS `temp`
ORDER BY `location` ASC;
I have entries which might or might not be assigned to a topic.
I want to select entries with the highest score by summing up the score of those, which belong to the same topic. In addition, I want to get a number of similar entries, which were grouped together.
SELECT *, COUNT(`topic_id`) FROM `entries`
GROUP BY `topic_id` HAVING `topic_id` > 0
ORDER BY SUM(`score`) DESC LIMIT 30
This query misses a few things. Firstly, I want entries without topic_id (topic_id = 0) not to be grouped, but to be treated individually. Secondly, COUNT(topic_id) does not always return a real number of entries belonging to the same topic.
SELECT * FROM
(
SELECT topic_id,COUNT(*),SUM(`score`) AS sum
FROM `entries`
WHERE `topic_id` > 0
GROUP BY `topic_id`
UNION
SELECT topic_id, 1,1 AS sum
FROM `entries`
WHERE `topic_id` = 0
)
ORDER BY sum DESC LIMIT 30
SELECT
`topic_id` > 0 IsGroupedTopid,
if(`topic_id`>0, `topic_id`, id) TopicOrEntryId,
COUNT(*) CountEntries,
SUM(`score`) TotalScore
FROM `entries`
GROUP BY
`topic_id` > 0,
if(`topic_id`>0, `topic_id`, id)
ORDER BY SUM(`score`) DESC
LIMIT 30;