SQL PHP Display values from database in Groups with two Columns [duplicate] - php

This question already has an answer here:
How can i list has same id data with while loop in PHP? [closed]
(1 answer)
Closed 8 years ago.
I have table called myTable and it looks like:
restid | menu | prod | price
__________________________________________
10 | m1 | prod1 | 15
10 | m1 | prod2 | 21
10 | m2 | prod3 | 36
10 | m2 | prod4 | 45
10 | m3 | prod5 | 12
10 | m3 | prod6 | 5
and I want to group and display the results as below:
m1
_________________________________________
prod1 | 15
prod2 | 21
_________________________________________
m2
_________________________________________
prod3 | 36
prod4 | 45
_________________________________________
m3
_________________________________________
prod5 | 12
prod6 | 5
I already grouped them using group_concat
SELECT DISTINCT `menu`, GROUP_CONCAT( DISTINCT `prod`
ORDER BY `prod` ) AS prod_list
FROM `myTable` WHERE restid = '10'
GROUP BY `menu`
ORDER BY `menu`
and then used php to display it properly...
but I want to include price in my results. I've been searching for an answer, but any ideas been found.
I'd be great if someone could give me some solution what can be used to achieve that result.

You can just add the price column in selection list. Somthing like this:-
SELECT DISTINCT `menu`, GROUP_CONCAT( DISTINCT `prod`
ORDER BY `prod` ) AS prod_list, `price`
FROM `myTable` WHERE restid = '10'
GROUP BY `menu`,`price`
ORDER BY `menu`;

Related

Selecting max value while group by two columns with order on another column

I have table with 4 rows
id season_id market elements I would like to select for each same season_id, market that hold max result and if max results are the same then max based on elements only where elements are higher than 9.
id | season_id | market | result | elements
1 | 20 | fh | 75 | 20
2 | 20 | fh | 75 | 22
3 | 20 | SH | 81 | 18
4 | 20 | SH | 75 | 20
5 | 21 | fh | 90 | 14
6 | 21 | fh | 86 | 16
7 | 21 | SH | 90 | 18
8 | 21 | SH | 91 | 2
I would like to get
id | season_id | market | result | elements
2 | 20 | fh | 75 | 22
3 | 20 | SH | 81 | 18
5 | 21 | fh | 90 | 14
7 | 21 | SH | 90 | 18
I've tried
SELECT a.* FROM results a INNER JOIN (SELECT id, market, MAX(result) as perc FROM
results where elements>9 group by market ) group ON a.market = group.market and
a.result = group.perc group by market
But it doesn't select all the markets and I'm not sure how to add selection by number of elements to that
You seem to want one result per season_id/market pair. I'm not 100% sure what the limit on elements > 9 is supposed to be doing, but I think it is an overall filter.
To get the rows with the maximum result and elements for each season and market, use row_number():
select t.*
from (select t.*,
row_number() over (partition by season_id, market order by result desc, elements desc) as seqnum
from t
where elements > 9
) t
where seqnum = 1;
Here is a db<>fiddle.
In older versions of MySQL (or even more recent versions), you can use a correlated subquery:
select t.*
from t
where t.id = (select t2.id
from t t2
where t2.season_id = t.season_id and t2.market = t.market and
t2.elements > 9
order by t2.result desc, t2.elements desc
limit 1
);

Using LIMIT within GROUP BY to get N results per dynamic group

Using LIMIT within GROUP BY to get N results per dynamic group
Hello everyone, firstly I read about questions like this problem.
But didn't get the solution. All of this SQL's are designed for static columns.
But I have dynamic columns.
Table:
id Name Group Level
2 Jonathan A 5
5 David A 10
6 Alex C 10
7 Kemal A 71
8 John D 21
9 Celin F 100
12 Alexis G 15
13 Noone A 23
I want to get the first 2 highest Level from each group.
But query must be dynamic because there will be more Groups, which is where I am stuck.
Solutions I tried:
Select the top N rows from each group Not giving true result it's broken.
Only work in static columns.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,name VARCHAR(12) NOT NULL
,group_name CHAR(1) NOT NULL
,level INT NOT NULL
);
INSERT INTO my_table VALUES
( 2,'Jonathan','A',5),
( 5,'David','A',10),
( 6,'Alex','C',10),
( 7,'Kemal','A',71),
( 8,'John','D',21),
( 9,'Celin','F',100),
(12,'Alexis','G',15),
(13,'Noone','A',23);
SELECT id
, name
, group_name
, level
FROM
( SELECT x.*
, CASE WHEN #prev = group_name THEN #i:=#i+1 ELSE #i:=1 END i
, #prev:=group_name
FROM my_table x -- technically, ordering should really happen here, in a separate subquery
, ( SELECT #prev:=null,#i:=0 ) vars
ORDER
BY group_name
, level DESC
, id
) a
WHERE i <=2;
+----+--------+------------+-------+
| id | name | group_name | level |
+----+--------+------------+-------+
| 7 | Kemal | A | 71 |
| 13 | Noone | A | 23 |
| 6 | Alex | C | 10 |
| 8 | John | D | 21 |
| 9 | Celin | F | 100 |
| 12 | Alexis | G | 15 |
+----+--------+------------+-------+
You can also do workaround.
Select colums upto 2 rows
FROM TABLE ORDER BY DESCENDING GROUP LEVEL
regards,
Umar Abdullah

MySQL and PHP: Getting a SUM from a row in column A based on a DATE from column A joined by an ID in column B?

So, as the title says, I think I want to get the SUM of a row in column A (Meta Value) based on matching DATEs in column A (Meta Value) joined from IDs in column B (Item ID).
Essentially, I want to look for a specific date 2017-05-05 in Meta Value. Then, when a date is a match, find the Item ID. In the example below, this would be 2 and 3. Then, get the SUM of the Field ID (11) for both Item ID 2 and Item ID 3 and return the SUM to a variable in PHP.
Here is what my data looks like:
+-----------+------------+------------+
| Meta Value| Field ID | Item ID |
+-----------+------------+------------+
| John | 8 | 1 |
|john#e.com | 10 | 1 |
| 2 | 11 | 1 |
|2016-11-20 | 12 | 1 |
| Mary | 8 | 2 |
|mary#e.com | 10 | 2 |
| **5** | 11 | 2 |
|2017-05-05 | 12 | 2 |
| Mike | 8 | 3 |
|mike#e.com | 10 | 3 |
| **2** | 11 | 3 |
|2017-05-05 | 12 | 3 |
+-----------+------------+------------+
I am after the SUM of 7 from Mike and Mary.
My current wordpress php call looks like this (but only gets me row count):
$bookings = $wpdb->get_var("SELECT COUNT(*) FROM wp_frm_item_metas WHERE field_id=12 AND meta_value='$select_date'");
Any help is appreciated!
seems you need sum on a self join
select sum(a.field_id)
from wp_frm_item_metas a
inner join wp_frm_item_metas b on b.item_id = a.item_id
and b.meta_value = '$select_date'
but if you need the sum for meta_value (11) you should
select sum(a.meta_vale)
from wp_frm_item_metas a
inner join wp_frm_item_metas b on b.item_id = a.item_id
and b.meta_value = '$select_date'
and a.filed_id =11

mysql select all unique rows in one column and all max rows in another column by datetime

What I need is to get the most recent (by date_time) unique player_id for each table_id
Table:
buyin_id player_id table_id date_time
---------|-----------|----------|--------------------|
1 | 10 | 21 | 2015-01-26 00:00:01
2 | 11 | 21 | 2015-01-26 00:00:02
3 | 12 | 21 | 2015-01-26 00:00:03
4 | 10 | 21 | 2015-01-26 00:00:04
5 | 11 | 21 | 2015-01-26 00:00:05
6 | 12 | 22 | 2015-01-26 00:00:06
7 | 13 | 22 | 2015-01-26 00:00:07
8 | 13 | 22 | 2015-01-26 00:00:08
Desired result:
buyin_id player_id table_id date_time
---------|-----------|----------|--------------------|
3 | 12 | 21 | 2015-01-26 00:00:03
4 | 10 | 21 | 2015-01-26 00:00:04
5 | 11 | 21 | 2015-01-26 00:00:05
6 | 12 | 22 | 2015-01-26 00:00:06
8 | 13 | 22 | 2015-01-26 00:00:08
I tried something like this which returns only 1 row instead of 1 row per table_id
SELECT pb.buyin_id, pb.player_id, pb.buyin, pb.cashout, pb.cashout_error, pb.date_time
FROM poker_buyin AS pb
INNER JOIN (SELECT player_id, MAX(date_time) AS MaxDateTime
FROM poker_buyin GROUP BY player_id) groupedpb
ON pb.player_id = groupedpb.player_id
AND pb.date_time = groupedpb.MaxDateTime
WHERE pb.player_id = '$player_id'";
The query you've mentioned finds the most recent record for each player id. And then you filter it to find just one player, so you get one row.
If you want to find the most recent record for each player and table, your inner query needs to be this:
SELECT MAX(buyin_id) buyin_id
FROM poker_buyin
GROUP BY player_id, table_id
This will get the row ids from your table that represent the latest player / table combinations.
Then you use that to pull records from your table, like this (http://sqlfiddle.com/#!2/be68b7/2/0)
SELECT whatever_columns
FROM poker_buyin
WHERE buyin_id IN
(
SELECT MAX(buyin_id) buyin_id
FROM poker_buyin
GROUP BY player_id, table_id
)
WHERE player_id = '$player_id'
ORDER BY player_id, table_id
There's a little trick in this query: The buyin_id value continually goes up, so it's a nice way of selecting the latest-in-time records for each combination.
if you don't need buyin_id in result columns that is simple:
SELECT DISTINCT player_id, table_id, max(date_time) as dt
FROM `poker_buyin `
GROUP BY player_id, table_id

Want to execute one complex Mysql query [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 9 years ago.
I want to get the result set only by one query if possible
i have table like that
------------------------------
user_id | activity_id | score
------------------------------
1 | 1 | 100
------------------------------
1 | 1 | 50
------------------------------
1 | 2 | 20
------------------------------
1 | 3 | 10
------------------------------
1 | 3 | 50
------------------------------
2 | 1 | 300
------------------------------
3 | 3 | 10
------------------------------
3 | 2 | 40
------------------------------
All what i need to select each user with sum of all of his high score in each activity.
for example the result set should be like that:
------------------------------
user_id | score
------------------------------
1 | 170
------------------------------
2 | 300
------------------------------
3 | 50
------------------------------
To get the highest score for each user/activity pair, you would use the following query:
select user_id, activity_id, max(score) as highscore
from t
group by user_id, activity_id;
To add these up for a given user, you would make this a subquery and do another aggregation:
select user_id, sum(highscore)
from (select user_id, activity_id, max(score) as highscore
from t
group by user_id, activity_id
) t
group by user_id;

Categories