Laravel / Mysql groupby ordering - php

I have the command
SELECT * FROM
(SELECT * FROM episodes ORDER BY created_at DESC) and t1
GROUP BY serial_id
ORDER BY created_at DESC
which I will return series where the last episode added by created_at. But the problem is that when I had it on hosting everything worked but now I put it on the VPS server and there I'm always lists the first episode. If i add desc after group by serial_id it's work but it is not possible on laravel (-> groupBy ('serial_id'))
Shared hosting:
+---------+-------------+--------+
| id | serial_id | part |
+---------+-------------+--------+
| 8124 | 12 | s02e10 |
| 362 | 8 | s09e12 |
| 4673 | 9 | s01e12 |
| 871 | 4 | s03e24 |
+---------+-------------+--------+
My VPS:
+---------+-------------+--------+
| id | serial_id | part |
+---------+-------------+--------+
| 8124 | 12 | s01e01 |
| 362 | 8 | s01e01 |
| 4673 | 9 | s01e01 |
| 871 | 4 | s01e01 |
+---------+-------------+--------+
Thank you in advance for your help

Seems you have a lot of errors in your query.
missing a tablename between FROM and ORDER BY
SELECT * FROM (SELECT * FROM
-- ^^ where is the tablename ?
ORDER BY episodes created_at DESC) and t1 GROUP BY ORDER BY serial_id created_at DESC
-- ^^ missing comma, ^^ this 'and' seems in the wrong place and you havent't column for group by and in last another missing comma
try something like this:
SELECT * FROM ( SELECT *
FROM my_table ORDER BY episodes, created_at DESC) t1
GROUP BY my_column_for_group_by
ORDER BY serial_id, created_at DESC
In laravel you could assign the order direction using:
->orderBy('serial_id')
->orderBy('created_at', 'DESC')

Probably you will have to use RAW in this case: groupBy(DB::raw('serial_id desc'))
like below
DB::table( DB::raw("(SELECT * FROM episodes ORDER BY created_at DESC) and t1") )->groupBy(DB::raw('serial_id desc'))->orderBy('created_at', 'desc')->get();

Related

SELECT, group, limit based on month

Currently I am using this: SELECT ident,COUNT(*) FROM sales GROUP BY ident order by COUNT(*) DESC LIMIT 3.
I use that to group each ident from sales table, and count the number of rows for each ident, and limit this to 3 rows. Now I want to only select the rows that were added the current month.
This is how the table looks like, sales table:
| ID | ident | prdnr | transfer |
| 1 | HD762 | 7362781 | 2020-08-10 16:25:26 |
| 2 | JJ313 | 4563456 | 2020-08-08 16:25:26 |
| 3 | HD762 | 4363453 | 2020-08-08 16:25:26 |
| 4 | JJ313 | 2326256 | 2020-08-08 16:25:26 |
| 5 | HD762 | 8356345 | 2020-08-07 16:25:26 |
| 6 | JJ844 | 3473563 | 2020-08-07 16:25:26 |
I think this should be the correct query:
SELECT ident,COUNT(*) FROM sales WHERE MONTH(transfer) = MONTH(CURRENT_DATE()) AND YEAR(transfer) = YEAR(CURRENT_DATE()) GROUP BY ident order by COUNT(*) DESC LIMIT 3
Somehow this does not gives me the correct count. What am I doing wrong?
Based on your requirement , a simple way is getting the MySQL current time and subtract a month to get the values
The code :-
SELECT ident,COUNT() FROM sales WHERE transfer > date_sub(now(),
interval 1 month) GROUP BY ident order by COUNT() DESC LIMIT 3;
The output from the query
HD762 3
JJ313 2
JJ844 1
Please let me know if this is the expected result you're looking for.
Reference :-
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

MySQL, Merge selects in order of one record from each select

I have a table that contains too many records and each bunch of records belong to someone:
---------------------
id | data | username
---------------------
1 | 10 | ali
2 | 11 | ali
3 | 12 | ali
4 | 20 | omid
5 | 21 | omid
6 | 30 | reza
now I want to create a query to result me like this:
1-10-ali
4-20-omid
6-30-reza
2-11-ali
5-21-omid
3-12-ali
Is there anyway to create a query to result me one record per each username and then one from another, and another to the end?
Unfortunately MySQL doesn't have a ranking system so you can use UDV (user defined variables) to rank your records like so.
SELECT id, `data`, name
FROM
( SELECT
id, `data`, name,
#rank := if(#name = name, #rank + 1, 1) as rank,
#name := name
FROM test
CROSS JOIN (SELECT #rank := 1, #name := '') temp
ORDER BY name, `data`
) t
ORDER BY t.rank, t.name, t.data
Sql Fiddle to play with
Output:
+---------------------+
| id | data | name |
+-----+------+--------+
| 1 | 10 | ali |
+---------------------+
| 4 | 20 | omid |
+---------------------+
| 6 | 30 | reza |
+---------------------+
| 2 | 11 | ali |
+---------------------+
| 5 | 21 | omid |
+---------------------+
| 3 | 12 | ali |
+---------------------+
The classic SQL approach is a self join and grouping that lets you determine a row's ranking position by counting the number of rows that come before it. As this is probably slower I doubt I could talk you out of the proprietary method but I mention it to give you an alternative.
select t.id, min(t.`data`), min(t.username)
from test t inner join test t2
on t2.username = t.username and t2.id <= t.id
group by t.id
order by count(*), min(t.username)
Your example would work with
SELECT id, `data`, name
FROM tbl
ORDER BY `data` % 10,
username
`data`;
If data and username do not have the desired pattern, then improve on the example.

MySQL. Select last record (by created time) from second table

I have 2 tables links by userPhone.
Table users
+-----------+-------+----------+--------+
| userPhone | name | address | car |
+-----------+-------+----------+--------+
| 096111111 | Bill | adr1 | {json} |
+-----------+-------+----------+--------+
| 097333333 | Max | adr2 | {json} |
+-----------+-------+----------+--------+
| 098888888 | Denis | adr3 | {json} |
+-----------+-------+----------+--------+
Table Orders
+-----------+---------+-------+-------+------------+
| userPhone | orderID | title | Descr | createdTS |
+-----------+---------+-------+-------+------------+
| 096111111 | 59 | ttl1 | qqqq | 1444999740 |
+-----------+---------+-------+-------+------------+
| 096111111 | 58 | ttl2 | wwww | 1444999650 |
+-----------+---------+-------+-------+------------+
| 096111111 | 56 | ttl3 | rrrrr | 1444999600 |
+-----------+---------+-------+-------+------------+
| 096111111 | 57 | ttl4 | ttttt | 1444999540 |
+-----------+---------+-------+-------+------------+
I'm using request like that, and it works well:
SELECT
`users`.`userPhone`,
`users`.`name`,
`users`.`address`,
`users`.`car`,
(SELECT `title` FROM `orders` WHERE `orders`.`userPhone` = `users`.`userPhone` AND `orders`.`orderTS` < NOW() ORDER BY `orders`.`createdTS` DESC LIMIT 1 ) as `title`,
(SELECT `descr` FROM `orders` WHERE `orders`.`userPhone` = `users`.`userPhone` AND `orders`.`orderTS` < NOW() ORDER BY `orders`.`createdTS` DESC LIMIT 1 ) as `descr`
FROM
`users`
LEFT JOIN `orders` USING (`userPhone`)
WHERE
`users`.`userPhone` like '%1111%';
BUT I think additional SELECTs work slow. Is there a better and faster way to solve it?
How to get same result using another request (probably without additional SELECTs).
A correctly syntaxed JOIN will do all that for you, without the need for the inner SELECT's and much quicker.
SELECT
`users`.`userPhone`,
`users`.`name`,
`users`.`address`,
`users`.`car`,
`orders`.`title`,
`orders`.`desc`
FROM `users`
JOIN `orders` ON `orders`.`phone` = `users`.`phone`
WHERE
`users`.`userPhone` like '%1111%'
AND `orders`.`orderTS` < NOW()
ORDER BY `orders`.`orderTS` DESC
LIMIT 1;
If you want all these record then just remove the LIMIT 1
You don't need to JOIN or LEFT JOIN any table if you're using an inner SELECT
Try this
SELECT
`users`.`userPhone`,
`users`.`name`,
`users`.`address`,
`users`.`car`,
(SELECT `title` FROM `orders` WHERE `orders`.`userPhone` = `users`.`userPhone` AND `orders`.`orderTS` < NOW() ORDER BY `orders`.`createdTS` DESC LIMIT 1 ) as `title`,
(SELECT `descr` FROM `orders` WHERE `orders`.`userPhone` = `users`.`userPhone` AND `orders`.`orderTS` < NOW() ORDER BY `orders`.`createdTS` DESC LIMIT 1 ) as `descr`
FROM
`users`
WHERE
`users`.`userPhone` like '%1111%';
This should be faster

MySQL query the same table with two date fields and group by same ID

I'm having a hard time getting results from a DB the way I need them, this is my table:
+----+---------------------+---------------------+-----------+------------+--------+
| id | opendate | closedate | openprice | closeprice | sameid |
+----+---------------------+---------------------+-----------+------------+--------+
| 1 | 2015-09-20 19:17:52 | NULL | 10 | NULL | 20 |
| 2 | NULL | 2015-09-20 20:17:00 | NULL | 35 | 20 |
| 3 | 2015-09-15 19:17:52 | NULL | 15 | NULL | 10 |
| 4 | NULL | 2015-09-16 20:17:00 | NULL | 25 | 10 |
+----+---------------------+---------------------+-----------+------------+--------+
I need to get all the rows grouped by the column sameid like this:
+----+---------------------+---------------------+-----------+------------+--------+
| id | opendate | closedate | openprice | closeprice | sameid |
+----+---------------------+---------------------+-----------+------------+--------+
| 1 | 2015-09-20 19:17:52 | 2015-09-20 20:17:00 | 10 | 35 | 20 |
| 3 | 2015-09-15 19:17:52 | 2015-09-16 20:17:00 | 15 | 25 | 10 |
+----+---------------------+---------------------+-----------+------------+--------+
And this is what I have tried so far:
(SELECT * FROM table WHERE opendate >= '2015-08-08 00:00:01') UNION (SELECT * FROM table WHERE closedate <= '2015-10-15 23:59:59')
I can get all the rows but I can not fin a way to group them by sameid, I tried using GROUP BY sameid without success.
Hope you guys can help me
Thank you
UPDATE
The table was designed that way long ago, (not by me) and there is too much information stored, I'm not allowed to redesign the DB schema either.
If your table format exactly what you describe, then this should work:
SELECT id, MAX(opendate), MAX(closedate), MAX(openprice), MAX(closeprice), sameid FROM table GROUP BY sameid;
However, I think you should redesign your database schema, seperate open info and close info into 2 rows (they could be in 2 tables or in same table). It's would be better for you to work with rather than trying some workaround.
Regards,
Try this
SELECT t1.opendate, t2.closedate, t1.openprice, t2.closeprice FROM `table` t1 JOIN `table` t2 ON t2.sameid = t1.sameid WHERE t1.opendate >= '2015-08-08 00:00:01' AND t2.closedate <= '2015-10-15 23:59:59' AND t1.opendate IS NOT NULL AND t2.closedate IS NOT NULL
This will produce your exact output.
SELECT
MIN(id) as id,
MAX(open_date) as open_date,
MAX(close_date) as close_date,
MAX(open_price) as open_price,
MAX(close_price) as close_price,
sameid
FROM
`table`
GROUP BY
sameid
ORDER BY id ASC

how use order by clause in group by statement

I have two table tbl_issue_log, tbl_magazine_issue
tbl_issue_log
============
+----------+--------+--------+-----------+---------------------+
| issue_id | mag_id | log_id | operation | updated_time |
+----------+--------+--------+-----------+---------------------+
| 2 | 1 | 1 | 1 | 2014-01-30 21:29:44 |
| 3 | 4 | 1 | 1 | 2015-01-30 21:29:44 |
| 2 | 1 | 1 | 3 | 2015-01-31 21:29:44 |
+----------+--------+--------+-----------+---------------------+
tbl_magazine_issue
=================
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
| ISSUE_ID | ISSUE_NAME | MAGAZINE_ID | COVER_PAGE_THUMB | FROM_DATE | TO_DATE | issue_description | login_page_no |
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
| 2 | test issue | 1 | cover page | 2014-01-30 | 2015-01-30 | sdssdg fsdf | 20 |
| 3 | test issue1 | 4 | cover page1 | 2014-01-30 | 2015-01-30 | sdssdg fsdf | 20 |
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
in tbl_issue_log contain multiple records for same issue id. i want only one issue at a time
and this must latest updated time.
My query is this
SELECT
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
GROUP BY tbl_issue_log.issue_id
ORDER BY tbl_issue_log updated_time DESC;
Here i got issue id based output . But not getting the latest updated timeed record.
If any one about this please help me.
Try this
SELECT * FROM
(SELECT
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
ORDER BY tbl_issue_log.updated_time DESC ) TEMP_TABLE
GROUP BY ISSUE_ID
Can you try changing order by clause as
ORDER BY tbl_issue_log.updated_time DESC;
Edit ---
As you are grouping on issue_id, mysql will select first row that matches the issue_id. The order by runs later which essentially does not return what you are looking for. You may need to use a subquery approach for this.
select some_table.* FROM
(
SELECT
MAX(tbl_issue_log.updated_time) AS updated_time,
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
GROUP BY tbl_issue_log.issue_id
) some_table
ORDER BY some_table.updated_time DESC;

Categories