ORDER BY result of SQL query - php

I have two tables:
'posts' (where 'post_text' field rows are comments users have posted); and
'votes' (the users' votes (thumbs up/down) on the posts, where 'post_id' matches 'id' from the 'posts' table):
SELECT *
FROM `posts`
+----+-----------+
| id | post_text |
+----+-----------+
| 1 | test0 |
| 2 | test1 |
| 3 | test2 |
| 4 | test3 |
| 5 | test4 |
| 6 | test5 |
| 7 | test6 |
| 8 | test7 |
| 9 | test8 |
| 10 | test9 |
| 11 | test10 |
+----+-----------+
SELECT *
FROM `votes`
+----+---------+--------+
| id | post_id | rating |
+----+---------+--------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 4 | 1 |
| 4 | 4 | 1 |
| 5 | 6 | 1 |
| 6 | 6 | 1 |
| 7 | 7 | 0 |
+----+---------+--------+
What I'd like to do is get all the 'post_text' values from 'posts' table, but sort them by highest no. of thumbs-up ('1') ratings first, then thumbs-downs ('0') to come next, then the posts without ratings (i.e. no corresponding votes in the 'votes' table) to come last. With a join, I can achieve this but I don't know how to get the 'post_text' values without ratings to also be in the result. This is what I got:
SELECT posts.id, post_id, rating, COUNT( * )
FROM posts
INNER JOIN votes ON posts.id = votes.post_id
GROUP BY post_id
ORDER BY rating DESC , COUNT( * ) DESC , post_id DESC
LIMIT 0 , 30
+----+---------+--------+----------+
| id | post_id | rating | COUNT(*) |
+----+---------+--------+----------+
| 6 | 6 | 1 | 2 |
| 4 | 4 | 1 | 2 |
| 1 | 1 | 1 | 1 |
| 7 | 7 | 0 | 1 |
| 2 | 2 | 0 | 1 |
+----+---------+--------+----------+

SELECT posts.id, posts.post_text, post_id, rating, COUNT( * )
FROM posts
LEFT JOIN votes ON posts.id = votes.post_id
GROUP BY post_id
ORDER BY rating DESC , COUNT( * ) DESC , post_id DESC
LIMIT 0 , 30

Related

PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table

Items Table
+----+------+
| id | name |
+----+------+
| 1 | abc |
| 2 | def |
| 3 | ghi |
+----+------+
Buy Table
+------+-------------+-------+---------+
| b_id | b_date | b_qty | b_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-01 | 10 | 1 |
| 2 | 2020-05-01 | 20 | 1 |
| 3 | 2020-05-02 | 5 | 2 |
| 3 | 2020-05-03 | 10 | 3 |
+------+-------------+-------+---------+
Rent Table
+------+-------------+-------+---------+
| r_id | r_date | r_qty | r_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-03 | 5 | 2 |
| 2 | 2020-05-03 | 10 | 2 |
| 3 | 2020-05-04 | 15 | 3 |
+------+-----------+---------+---------+
Sell Table
+------+-------------+-------+---------+
| s_id | s_date | s_qty | s_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-03 | 10 | 1 |
| 2 | 2020-05-05 | 20 | 3 |
| 3 | 2020-05-06 | 5 | 3 |
+------+-----------+---------+---------+
And I'm trying to get outputs with php foreach something like this ...
$trans_date
$buy_qty
$rent_qty
$sell_qty
$item
In case item id 1
+-------------+--------------+---------------+---------------+------+
| trans_date | buy_qty | rent_qty | sell_qty | item |
+-------------+--------------+---------------+---------------+------+
| 2020-05-01 | 30 | 0 | 0 | abc |
| 2020-05-02 | 0 | 0 | 0 | abc |
| 2020-05-03 | 0 | 0 | 10 | abc |
| 2020-05-04 | 0 | 0 | 0 | abc |
| 2020-05-05 | 0 | 0 | 0 | abc |
| 2020-05-06 | 0 | 0 | 0 | abc |
+-------------+--------------+---------------+---------------+------+
This is the query I've come for one table (b_date column has timestamp value)...
$query = $this->db->query("
SELECT FROM_UNIXTIME(b_date,'%d %M') AS date_b
, SUM(b_qty) AS qty_b
FROM buytable
WHERE b_itmid = 1
AND MONTH(FROM_UNIXTIME(b_date)) = MONTH(CURDATE())
GROUP
BY DATE(FROM_UNIXTIME(b_date))
");
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$result[] = $data;
}
return $result;
}
Finally I've found the correct answer https://stackoverflow.com/a/61875506/7554875
select d.date, b.qty_buy, r.qty_rent, s.qty_sell, i.name
from items i
cross join (
select b_date as date from buy
union all select r_date as date from rent
union all select s_date as date from sell
) d
left join (select b_date as date, b_itmid, sum(b_qty) qty_buy
from buy group by date, b_itmid) b
on b.date = d.date and b.b_itmid = i.id
left join (select r_date as date, r_itmid, sum(r_qty) qty_rent
from rent group by date, r_itmid) r
on r.date = d.date and r.r_itmid = i.id
left join (select s_date as date, s_itmid, sum(s_qty) qty_sell
from sell group by date, s_itmid) s
on s.date = d.date and s.b_itmid = i.id
where i.id = 1 and d.date >= date_format(curent_date, '%Y-%m-01')
order by d.date

Custom MySQL Ordering

I have a query that needs a custom sorting, trimmed down to the bare minimums something like:
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY product_id DESC
And I get returned a set of rows like:
UserID ProductID
2 5
2 4
3 3
1 2
1 1
But I want it to actually sort SOMETHING like this (so no 2 UserIDs are adjacent to eachother):
UserID ProductID
1 2
2 4
3 3
2 5
1 1
Is this even possible with MySQL, or do I need some PHP magic?
A canonical way of solving this problem is by enumerating the duplicate rows and then ordering by that value:
select t.*
from (SELECT u.*, p.*, p.id as product_id,
row_number() over (partition by u.id order by (select NULL)) as seqnum
FROM users u join
products p
on u.id = p.user_id
) t
order by seqnum, id;
This will work, as long as no one user has a really long sequence (as in your example).
There is no "always-works" solution, because it is easy to come up with a situation where your goal is not possible.
Here fetch your sorted results into an array. Then do something like this.
$records = $res->fetchAll();
$count = count($records);
$records = array_chunk($records, ceil(count($records)/2);
$unsorted = array();
for($x = 0; $x < $count; $x++){
$unsorted[] = $records[$x%2][floor($x/2)];
}
Consider the following...
CREATE TABLE sortable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,player_id INT NOT NULL);
INSERT INTO sortable(player_id) VALUES (1),(1),(2),(3),(4),(3),(3),(2),(1),(2),(4),(4);
SELECT * FROM sortable;
+----+-----------+
| id | player_id |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 3 |
| 7 | 3 |
| 8 | 2 |
| 9 | 1 |
| 10 | 2 |
| 11 | 4 |
| 12 | 4 |
+----+-----------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY player_id,rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 9 | 1 | 3 |
| 3 | 2 | 1 |
| 8 | 2 | 2 |
| 10 | 2 | 3 |
| 4 | 3 | 1 |
| 6 | 3 | 2 |
| 7 | 3 | 3 |
| 5 | 4 | 1 |
| 11 | 4 | 2 |
| 12 | 4 | 3 |
+----+-----------+------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 4 | 1 |
| 2 | 1 | 2 |
| 8 | 2 | 2 |
| 6 | 3 | 2 |
| 11 | 4 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 7 | 3 | 3 |
| 12 | 4 | 3 |
+----+-----------+------+
So if your problem is just that you dont want two records with same id should not come next to each other wha I can think simplest is use
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY user_id%2 DESC
Or you can even use other number than 2 to meet any certain order you want....

Removing Duplicated Entries: Delete a entry that has 2 the same column

i have this table called bag:
+--------+----------+---------+----------+
| bag_id | chara_id | item_id | item_qty |
+--------+----------+---------+----------+
| 1 | 1 | 2 | 22 |
| 2 | 1 | 1 | 55 |
| 3 | 3 | 1 | 2 |
| 6 | 3 | 4 | 2 |
| 7 | 4 | 4 | 2 |
| 8 | 5 | 4 | 2 |
| 9 | 6 | 4 | 2 |
| 10 | 1 | 5 | 1 |
| 11 | 1 | 2 | 1 |
| 12 | 1 | 2 | 1 |
| 13 | 1 | 2 | 1 |
| 14 | 1 | 8 | 1 |
| 15 | 1 | 6 | 1 |
| 16 | 1 | 8 | 1 |
| 17 | 1 | 6 | 1 |
+--------+----------+---------+----------+
the relationship goes as 1 chara = many item
now i dont want 1 chara = many duplicated item.
how can i make a query that delete's the duplicated values?
like chara_id: 1 has 3 duplicated item_id: 2
i want to delete the other 2.
Not the best way to do it. But the below should definetly work:
Delete from Bag
where bag_id
not in (
select min(bag_id) from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) > 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
UNION
select bag_id from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) = 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
)
You can simply join table bag with a subquery which gets the minimum bag_id for every combination of chara_ID and item_ID. Records that have null values on any fields on the subquery are the records that will be deleted.
DELETE a
FROM bag a
LEFT JOIN
(
SELECT chara_ID, item_ID, MIN(bag_ID) min_ID
FROM bag
GROUP BY chara_ID, item_ID
) b ON a.bag_ID = b.min_ID AND
a.chara_ID = b.chara_ID AND
a.item_ID = b.item_ID
WHERE b.min_ID IS NULL
SQLFiddle Demo

MySQL Select top two people with most submitted videos, where type=1

I have a table of videos submitted by people:
+----+-----------+------+---------+
| id | by_person | type | title |
+----+-----------+------+---------+
| 1 | 3 | 1 | title1 |
| 2 | 4 | 1 | title2 |
| 3 | 3 | 1 | title3 |
| 4 | 4 | 2 | title4 |
| 5 | 3 | 1 | title5 |
| 6 | 6 | 2 | title6 |
| 7 | 6 | 2 | title7 |
| 8 | 4 | 2 | title8 |
| 9 | 3 | 1 | title9 |
| 10 | 4 | 1 | title10 |
| 11 | 4 | 1 | title11 |
| 12 | 3 | 1 | title12 |
+----+-----------+------+---------+
How do I SELECT the top two people who have submitted the most videos with type=1 so I get a presentation like this?
1. Person(3) - 5 videos
2. Person(4) - 3 videos
I think this will work:
SELECT by_person, count(*) AS total
FROM videos
WHERE type = 1
GROUP BY by_person
ORDER BY total DESC
LIMIT 2
DEMO: http://sqlfiddle.com/#!2/b2916/22
You can try this:
select by_person, sum(case when type = 1 then 1 else 0 end) as NumType1
from videos v
group by by_person
order by NumType1 desc
limit 2
SELECT by_person, COUNT(title)
FROM videos
WHERE type = 1
GROUP BY by_person
ORDER BY COUNT(title) DESC LIMIT 2;

Select tag with count from multiple table join containing multiple tags for each id

My table has 4 tags linked to the to each id. I want to select the position(rank) of that value(tag_name or tag_id) and the number of times the value(tag_name or tag_id) displays in that same position(rank).
Here's what it would look like in mysql:
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | cookie | 2 | 1 |
> | 2345 | bar | 1 | 2 |
> | 2345 | cereal | 3 | 3 |
> | 2345 | milk | 4 | 4 |
> | 2346 | cereal | 3 | 1 |
> | 2346 | milk | 4 | 2 |
> | 2346 | cookie | 2 | 3 |
> | 2346 | hot dogs | 5 | 4 |
> | 2347 | chocolate | 6 | 1 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Here's what my current code looks like:
SELECT m.*, tr.tag_id, t.tag_name, #rownum:=#rownum + 1 AS rank
FROM meals AS m
RIGHT JOIN tags_rel AS tr ON tr.meal_id = m.id
JOIN tags AS t ON tr.tag_id = t.id
JOIN (SELECT #rownum:=0) AS r
ORDER BY m.id DESC
Please keep in mind that I'm actually using a pagination so my ORDER BY actually looks like this:
ORDER BY id DESC LIMIT $start_from, 12
If I select either tag_name=bar or tag_id=1 and rank=2 I should get the bellow results. Plus I would like the count of rows returned for that value.
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | bar | 1 | 2 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Thanks!
SELECT tag_name, tag_id, rank, COUNT(tag_id) AS rankcount
FROM table
GROUP BY tag_id, rank;
results in:
+-----------+--------+------+-----------+
| tag_name | tag_id | rank | rankcount |
+-----------+--------+------+-----------+
| bar | 1 | 2 | 2 |
| cookie | 2 | 1 | 1 |
| cookie | 2 | 3 | 1 |
| cereal | 3 | 1 | 1 |
| cereal | 3 | 3 | 1 |
| milk | 4 | 2 | 1 |
| milk | 4 | 4 | 1 |
| hot dogs | 5 | 4 | 1 |
| chocolate | 6 | 1 | 1 |
+-----------+--------+------+-----------+
9 rows in set (0.00 sec)
If you want your original table enhanced by a count of lines (although that seems to be quite senseless, as every sql result returns the number of rows) you'll have to do a subquery:
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table;
+------+-----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+-----------+------+------+---------------+
| 2345 | cookie | 2 | 1 | 2 |
| 2345 | bar | 1 | 2 | 2 |
| 2345 | cereal | 3 | 3 | 2 |
| 2345 | milk | 4 | 4 | 2 |
| 2346 | cereal | 3 | 1 | 2 |
| 2346 | milk | 4 | 2 | 2 |
| 2346 | cookie | 2 | 3 | 2 |
| 2346 | hot dogs | 5 | 4 | 1 |
| 2347 | chocolate | 6 | 1 | 1 |
| 2347 | bar | 1 | 2 | 2 |
+------+-----------+------+------+---------------+
10 rows in set (0.00 sec)
Extending that query would result in your desired result, extended by a column where the count of rows is inserted (which is IMO still rather senseless).
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table
WHERE tag_id = 1;
results in
+------+----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+----------+------+------+---------------+
| 2345 | bar | 1 | 2 | 2 |
| 2347 | bar | 1 | 2 | 2 |
+------+----------+------+------+---------------+
2 rows in set (0.00 sec)
how about this:
SELECT DISTINCT meal_id as MealId, t2name as TagName, t2id as TagId, Rank
FROM
(SELECT t1id as t2id, t1name as t2name, rnk as rnk2, count(*) as Rank
FROM
tags_rel AS r,
(SELECT m1id, t1id, t1name,
#rnk := CASE WHEN #id <> m1id THEN 1 ELSE #rnk + 1 END AS rnk, #id := m1id as idx
FROM
tags_rel AS r,
(SELECT #id := 0) idx,
(SELECT #rnk := 0) rxx,
(SELECT m.id AS m1id, t.id AS t1id, name AS t1name
FROM tags_rel r
JOIN (meals AS m, tags AS t ) ON ( m.id = r.meal_id AND t.id = r.tag_id )
) AS rn
WHERE r.tag_id = t1id AND r.meal_id = m1id
ORDER BY m1id, t1name
) AS rno
WHERE r.tag_id = t1id AND r.meal_id = m1id
group BY t1id, t1name, rnk
) as rn2
join (meals, tags_rel) on (meals.id = tags_rel.meal_id and tags_rel.tag_id = t2id)
ORDER BY meal_id, t2id
which results in:
2345 bar 1 2
2345 cookie 2 1
2345 cereal 3 1
2345 milk 4 2
2346 cookie 2 1
2346 cereal 3 1
2346 milk 4 2
2346 hotdog 5 1
2347 bar 1 2
2347 chocolad 6 1

Categories