Im trying to select every lasted insert row with a match_id and show all match_id's on a php page, but not show multiple rows on same match_id.
Since it is a logging page for my kids football games i will insert multiple rows on same match_id to mark if he scores, so i wont be updating same match_id.
Here you can see a example how the database looks. I have some more columns also but just to get you the idea how it looks.
(AI)id match_id team1 team2 result1 result2 goal timenow
1 1 US ENG 0 0 0 00:00
2 1 US ENG 1 0 0 08:00
3 1 US ENG 1 1 0 13:00
4 1 US ENG 2 1 1 20:00
5 2 US FRA 0 0 0 00:00
6 2 US FRA 1 0 1 04:00
7 2 US FRA 1 1 0 26:00
8 3 US GER 0 0 0 00:00
9 3 US GER 1 0 0 16:00
The result i would like to get is a php page that lists just the last row of every unique match id.
Example html/php would show lika below:
US-ENG 2-1
US-FRA 1-1
US-GER 1-0
Hope you understands what i mean :)
EDIT: got it working by this line of code if somebody other have the same needs.
Thanks to Akilan who lead me in right direction :)
SELECT * FROM table WHERE id IN (SELECT MAX( id ) FROM table GROUP BY match_id) ORDER BY `table`.`timenow` DESC
You can do a order by with time and group by your match id can achieve the desired output right??
For example,
SELECT * FROM match_table GROUP BY match_id ORDER BY TIME(timenow)
Happy Coding,
AK
This will help you
select group_concat(team1,'-',team2,' ',result1,'-',result2) from match_table group by match_id order by match_id DESC LIMIT 1;
SELECT group_concat(t1.team1,'-',t1.team2,' ',t1.result1,'-',t1.result2) from table t1
LEFT JOIN table t2
ON (t1.matchid = t2.matchid AND t1.id < t2.id)
WHERE t2.id IS NULL;
Related
I have a little problem to solve, but I can't. I have the following chat table:
id user friend msg date
----------------------------------------------------------------------
1 1 2 Hello Bob! 2014-07-04 01:00
2 1 2 How are you doing? 2014-07-04 01:01
3 2 1 I'm fine bro! 2014-07-04 02:30
4 1 3 Hey Mark :D 2014-07-04 02:31
5 3 1 Yo! 2014-07-04 02:32
6 4 1 Wassup?! 2014-07-04 07:00
I'm working on a PHP getInbox($uid) method that returns an array of the last message of each pair (user and friend). I tried a SELECT query with GROUP BY friend, but it is incomplete.
SELECT * FROM `chat`
GROUP BY `friend`
WHERE `user` = $uid OR `friend` = $uid
The desired result is:
id user friend msg date
----------------------------------------------------------------------
3 2 1 I'm fine bro! 2014-07-04 02:30
5 3 1 Yo! 2014-07-04 02:32
6 4 1 Wassup?! 2014-07-04 07:00
I would appreciate a help!
SELECT c.* FROM
chat c
JOIN
(SELECT
max(id) max_id,
(CASE WHEN user < friend THEN user ELSE friend END) user_a,
(CASE WHEN user < friend THEN friend ELSE user END) user_b
FROM chat
GROUP BY user_a, user_b) t1 ON t1.max_id = c.id
The case statements select (user,friend) in ascending order. For example, both (1,2) and (2,1) will be converted to (1,2). An ordered pair uniquely identifies a conversation. Finally, the latest id is selected for each ordered pair and rows having those ids are displayed from the chat table.
Im having some trouble with this.
Here's an example of my tables.
Booking
id zone_id name excursion_id
1 2 1
2 1 1
3 2 1
The table where I have the quantities
booking_price
id_booking id_price quantity
1 1 2
1 2 3
2 1 1
2 2 0
3 1 2
3 2 3
Here the zone table
Zone
id Name
1 a
2 b
3 c
So I want to have a table like that
Zone_id Quantity
1 1
2 10
3 0
The problem is when im joining tables and filtering by excursion_id im not getting ALL the zones.
I want to know how many people goes in each zone.
I think better way of doing it is
select z.id, coalesce(sum(bp.quantity),0) as quantity
from Booking b
right join Zone z on z.id = b.zone_id AND b.excursion_id = 1
left join booking_price bp on bp.id_booking = b.id
group by z.id
demo http://sqlfiddle.com/#!2/771f5/13
You could use a query like:
SELECT zone.id as zone_id,
sum(quantity) as Quantity
FROM zone
LEFT JOIN Booking on Booking.zone_id = zone.id
LEFT JOIN Excursion on Excursion.id_booking = Booking.id
GROUP BY zone.id
Still very new to all of this so bear with me.
Have 3 tables
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table:
Do a query and gather all of the courses, find the max score for each course and attribute the member name to it.
Table result should look like:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table.
How about this query for SQL?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data.
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following
Course Name Score Member
Math 85 Mark
Try this query :
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC
I need to know if there is a possible way doing this with out subquery..
Here is my table structure:
id-name-father_id
1 joe 0
2 mark 0
3 muller 0
4 miki 2
5 timi 2
6 moses 2
7 david 1
8 momo 0
9 daniel 0
10 ermi 3
My table logic is
0 means he is not a child of some one
1+ mean that he is son of man in that row.
Note: if some one have a child, he still
will have 0 in father id (it's mean there is not grand-fathers in my table)
My query is :
SELECT id, name, count(id=father_id) as sons
WHERE father_id = 0
What I want to get is a list of non-children (father_id=0) and sum
the childrens it has.
Is there a way to get the results without a subquery?
This should do it (MySQL):
SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
FROM `people` AS parents
LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
WHERE `parents`.`father_id` = 0
GROUP BY `parents`.`id`
According to Gary we need to add name to GROUP BY in other SQL databases:
SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
FROM `people` AS parents
LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
WHERE `parents`.`father_id` = 0
GROUP BY `parents`.`id`, `parents`.`name`
We are joing the table with itself here. So we join all parents with their children.
This will lead to a result like that:
parents.id parents.name children.id children.name
1 joe 7 david
2 mark 4 miki
2 mark 5 timi
2 mark 6 moses
3 muller 10 ermi
8 momo - - # left join allows this line
9 daniel - -
But now we have each parent several times. So we are GROUP'ing the whole thing over the parent’s id, which will result in the following:
parents.id parents.name COUNT(children.*)
1 joe 1
2 mark 3
3 muller 1
8 momo 0
9 daniel 0
You should be able to do it without any joins or sub-queries as follows:
select case father_id when 0 then id else father_id end id,
max(case father_id when 0 then name end) name,
sum(sign(father_id)) sons
from table
group by case father_id when 0 then id else father_id
Here is a simplified version of my sql table of 2 months (ORDERED BY DATE):
player_id |
date |
score
1 2011-05-25
1200
2 2011-05-25
3400
3 2011-05-26
3200
4 2011-05-26
4400
1 2011-05-28
1000
2 2011-05-28
2000
3 2011-05-29
3000
4 2011-05-29
4000
1 2011-06-24
1300
2 2011-06-24
2500
3 2011-06-24
5000
4 2011-06-24
3000
Basically, I want a query that shows the last score of all players in a specific month/specific year.
Example:
If I want the final scores of all players in the month 05, te result
would be:
1 2011-05-28 1000
2 2011-05-28 2000
3 2011-05-29 3000
4 2011-05-29 4000
My sql query so far:
SELECT m1.* FROM table m1
LEFT JOIN table m2 ON (m1.player_id = m2.player_id AND m1.date < m2.date)
WHERE m2.date IS NULL
AND month(m1.date) = 05
AND year(m1.date) = 2011
ORDER BY score DESC);
This doesn't seem to show all players, only players that didn't play in the months after 05. Where do I add the date select?
**EDIT
John Nestoriak's answer bellow did the trick for me :)
I think he's referring to the technique shown here: Retrieving the last record in each group
With the additional constraint of he doesn't want the last record but the last record in a given month.
Oddly enough you have to give that additional constraint twice, once in the join condition and again to filter the results. This should do it for you.
SELECT m1.* FROM table m1
LEFT JOIN table m2 ON
(m1.player_id = m2.player_id AND m1.date < m2.date
AND m2.date < '2011-06-01')
WHERE m2.date IS NULL AND month(m1.date) = 5 AND year(m1.date) = 2011
Assuming that the (player_id, date) combination in Unique:
SELECT
t.*
FROM
TableX AS t
JOIN
( SELECT
player_id
, MAX(date) AS maxDate
FROM
TableX
WHERE
date BETWEEN '2011-05-01'
AND LAST_DAY('2011-05-01')
GROUP BY
player_id
) AS tg
ON
(tg.player_id, tg.maxDate) = (t.player_id, t.date)
ORDER BY
t.score DESC