Im coding a custom search box to find starcraft games, but i have no idea how to do the query. FOR EXAMPLE an user search "Player: daniel, who played: zerg vs a terran player on 'x' map"
So i have this tables:
Table games
+------------+----------+-------------+----------+
| game_id | map | match | winner |
+------------+----------+-------------+----------+
| 7 | y | 1v1 | 1 |
| 8 | x | 1v1v1 | 2 |
| 9 | w | 1v1v1 | 3 |
| 10 | x | 1v1 | 1 |
+------------+----------+-------------+----------+
Table players
+------------+----------+-------------+----------+
| game_id | player |Civilization | team |
+------------+----------+-------------+----------+
| 7 | Arturo | protos | 1 |
| 7 | Daniel | zerg | 2 |
| 8 | Ale | Terran | 1 |
| 8 | Maria | Protos | 2 |
| 8 | Daniel | zerg | 3 |
| 9 | Pablo | zerg | 1 |
| 9 | Ale | protos | 2 |
| 9 | Maria | protos | 3 |
| 10 | Daniel | zerg | 1 |
| 10 | Oscar | terran | 2 |
+------------+----------+-------------+----------+
With the correct query i must get :
Game_id: 8, 1v1v1, daniel (zerg) vs maria (protos) vs ale (terran), map: x
Game_id:10, 1v1, daniel (zerg) vs oscar (terran), map: x
The problem here is how i get the game WHERE exist a daniel player AND zerg civ AND WHERE an oponent have a terran civ in the same game_id??! And also the game is played in x map?
PD: As you notice each game can have different size of players. Please help. Very much appreciated
try this select g.game_id, GROUP_CONCAT(CONCAT(p.player,'(', p.Civilization, ')-',g.map) SEPARATOR ' vs ') from games as g join players p on p.game_id = g.game_id where g.map = 'x' group by g.game_id;
Related
I am firing a query in mysql but not getting desired output.
this is the code:
select team_name,
sum(semis.points+final.points) as final_points
from semis
inner join final on semis.sid=final.sid
inner join teams on teams.tid=semis.tid
group by semis.tid
union
select team_name,
semis.Points
from semis
inner join teams on semis.tid=teams.tid
left join final on semis.sid=final.sid
where final.sid is null;
OUTPUT:
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 7 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 11 |
| Gujarati | 10 |
| English | 10 |
| Economics | 20 |
| BCOM | 3 |
| Chemistry | 3 |
| English | 3 |
+-----------------------+--------------+
and the result i want to fetch
+-----------------------+--------------+
| team_name | final_points |
+-----------------------+--------------+
| BioTech & BioChem | 7 |
| Chemistry | 10 |
| Botany & Zoology | 7 |
| Physics & Electronics | 17 |
| BCA | 19 |
| BCOM | 14 |
| Gujarati | 10 |
| English | 13 |
| Economics | 20 |
+-----------------------+--------------+
Adding last 3 values to english,bcom,chemistry increasing it by 3 and making a total of BCOM: 14, Chemistry:10 , English: 13
From the sample data you posted and the expected results it looks like you can do it without UNION, by left joining final and with coalesce() for final.points:
select team_name, sum(semis.points + coalesce(final.points, 0)) as final_points
from semis
inner join teams on teams.tid=semis.tid
left join final on semis.sid=final.sid
group by semis.tid
So, I have 3 tables :
guest :
id_guest | name
1 | John
2 | Nick
3 | James
4 | Paul
5 | Chris
6 | Karen
7 | Peter
room :
id_room | status
1 | Clean
2 | Dirty
3 | Dirty
4 | Clean
5 | Clean
6 | Clean
reservation :
id_guest | id_room | date
1 | 1 | 2015-04-15
1 | 1 | 2015-04-16
1 | 1 | 2015-04-17
2 | 3 | 2015-04-15
3 | 4 | 2015-04-15
3 | 4 | 2015-04-16
4 | 2 | 2015-04-16
5 | 2 | 2015-04-17
6 | 2 | 2015-04-18
And this is what the expected output should be :
id_room | status | d04-15 | d04-16 | d04-17 | d04-18
1 | Clean | John | John | John |
2 | Dirty | | Paul | Chris | Karen
3 | Dirty | Nick | | |
4 | Clean | James | James | |
5 | Clean | | | |
6 | Clean | | | |
I have been able to show it until the third field (d04-15) though with the date as values, using :
SELECT room.id_room,
room.status,
reservation.date AS d04-15
FROM room
LEFT JOIN reservation
ON room.id_room = reservation.id_room AND reservation.date = '2015-04-15'
GROUP BY room.id_room
But I'm not sure as to how to display the name there and
appending new fields (d04-16, d04-17, and d04-18) from another JOIN statement.
Any help would be appreciated. Thanks.
The columns returned in a query can't be altered at run time; they have to be statically declared in the SQL SELECT statement.
Here's an example of a statement that can achieve the specified result:
SELECT m.id_room
, m.status
, MAX(IF(r.date='2015-04-15',g.name,NULL)) AS `d04-15`
, MAX(IF(r.date='2015-04-16',g.name,NULL)) AS `d04-16`
, MAX(IF(r.date='2015-04-17',g.name,NULL)) AS `d04-17`
, MAX(IF(r.date='2015-04-18',g.name,NULL)) AS `d04-18`
FROM room m
LEFT
JOIN reservation r
ON r.id_room = m.id_room
AND r.date IN ('2015-04-15','2015-04-16','2015-04-17','2015-04-18')
LEFT
JOIN guest g
ON g.id_guest = r.id_guest
GROUP BY m.id_room
How can I build a query to sort the results by vl_name (vldescription) alphabetically, and subsort table vllinks by vlk_addeddate from the latest with internal limit equal to 1?
SELECT
aa.vl_id, aa.vl_name, aa.vl_code, aa.vl_vcc, aa.vl_description,
bb.vlk_id, bb.vlk_vlid, bb.vlk_link, bb.vlk_platform, bb.vlk_location, bb.vlk_addeddate,
le.vop_vlid, le.vop_thumbnail,
cx.vcc_id, cx.vcc_type, cx.vcc_brand, cx.vcc_variant
FROM vldescription AS aa
LEFT JOIN vllinks AS bb ON aa.vl_id = bb.vlk_vlid
LEFT JOIN vlofferphotos AS le ON le.vop_vlid = aa.vl_id
LEFT JOIN vlcarcats AS cx ON cx.vcc_id = aa.vl_vcc
WHERE aa.vl_vcc = '$change_me_if_you_need'
GROUP BY vl_id
ORDER BY vl_name
Table vlcarcats (vcc_
vcc_id | vcc_type | vcc_brand | vcc_variant
1 | OpenPlace | SomeCorp1 | website
2 | ForPrive | SomeCorp2 | other way
Table vldescription
vl_id | vl_name | vl_code | vl_vcc | vl_description
1 | OpTECC | xDAOcm | 1023 | text, text,...
2 | NewCop | d9MMo2 | 42 | more text,...
Table vllinks (vlk_vlid == vl_id)
vlk_id | vlk_vlid | vlk_link | vlk_platform | vlk_location | vlk_addeddate
1 | 1 | http://... | 1 | USA | 2014-01-10
2 | 2 | http://... | 1 | UK | 2014-01-12
3 | 2 | ftp://... | 2 | UK | 2014-01-15
4 | 2 | ftp://... | 2 | India | 2014-01-19
5 | 1 | ftp://... | 2 | Austria | 2014-01-22
Table vlofferphotos (vop_vlid == vl_id)
vop_vlid | vop_thumbnail
1 | abcdefg.jpg
2 | hijklmn.jpg
I'm trying to 'CONCAT' the description of multiple rows in a single row.
I have these tables:
tb_employees:
+---------------+---------------+
| id_employee | employee |
+---------------+---------------+
| 1 | Robert Tomson |
| 2 | Jhonatan Weg |
| 3 | Eva Uhte |
+---------------+---------------+
tb_requirements:
+---------------+-----------------+
| id_requirem | requirem |
+---------------+-----------------+
| 11 | Photo |
| 12 | Criminal Record |
| 13 | Shooting Test |
+---------------+-----------------+
tb_details:
+-----------------+---------------------+-------------------------+
| id_detail | id_employee | id_requirem |
+-----------------+---------------------+-------------------------+
| 1 | 1 | 11 |
| 2 | 1 | 12 |
| 3 | 1 | 13 |
| 4 | 2 | 11 |
| 5 | 2 | 13 |
| 6 | 3 | 12 |
| 7 | 3 | 13 |
+-----------------+---------------------+-------------------------+
I have to make a SELECT query to show like this:
+-------------------------+----------------------------------------+
| employee | requirem |
+-------------------------+----------------------------------------+
| Robert Tomson | Photo - Criminal Record - Shooting Test|
| Jhonatan Weg | Photo - Shooting Test |
| Eva Uhte | Criminal Record - Shooting Test |
+-------------------------+----------------------------------------+
To be honest, i really don't know how :S
Any ideas? Thank you for answer.
Use GROUP_CONCAT()
SELECT e.employee, GROUP_CONCAT(r.requirem ORDER BY r.requirem SEPARATOR ' - ') requirements
FROM tb_employees e LEFT JOIN tb_details d
ON e.id_employee = d.id_employee JOIN tb_requirements r
ON d.id_requirem = r.id_requirem
GROUP BY e.id_employee, e.employee
Output:
| EMPLOYEE | REQUIREMENTS |
|---------------|-----------------------------------------|
| Robert Tomson | Criminal Record - Photo - Shooting Test |
| Jhonatan Weg | Photo - Shooting Test |
| Eva Uhte | Criminal Record - Shooting Test |
Here is SQLFiddle demo
Edit:
You should use GROUP_CONCAT and GROUP BY the requirem field.
You need to use group_concat Try the below :
SELECT group_concat(distinct(e.employee) separator ', ') , group_concat(r.requirem separator ', ')
FROM TB_DETAILS d
JOIN TB_EMPLOYEES e ON d.id_employee = e.id_employee
JOIN tb_requirements r ON d.id_requirem = r.id_requirem
| GROUP_CONCAT(DISTINCT(E.EMPLOYEE) SEPARATOR ', ') | GROUP_CONCAT(R.REQUIREM SEPARATOR ', ') |
|---------------------------------------------------|------------------------------------------|
| Robert Tomson, Jhonatan Weg, Eva Uhte | Photo, Criminal Record, Shooting Test, |
| | Photo, Shooting Test, Criminal Record, |
| | Shooting Test |
I have three existing SQL tables we will call "teams", "miles", and "riders". Leaving out the fluff, their structure looks like this:
Table: teams
------------+-------------+---------+
| team_name | captains_id | team_id |
------------+-------------+---------+
| superbads | 11 | 1 |
| superflys | 12 | 2 |
------------+-------------+---------+
Table: riders
--------------+-----------+----------+
| rider_name | team_id | rider_id |
--------------+-----------+----------+
| donatello | 1 | 10 |
| leonardo | 1 | 11 |
| michelangelo| 2 | 12 |
| raphael | 2 | 13 |
--------------+-----------+----------+
Table: miles
--------------+-----------+----------+
| rider_id | miles | id |
--------------+-----------+----------+
| 10 | 100 | 1 |
| 10 | 62 | 2 |
| 11 | 110 | 3 |
| 11 | 100 | 4 |
| 12 | 8 | 5 |
| 12 | 22 | 6 |
| 13 | 29 | 7 |
| 13 | 2 | 8 |
--------------+-----------+----------+
I need to return a list of teams with total miles generated by that team (I also need to return the team captain's name, but that's a bit easier).
The difficulty is that I need to join miles on riders, sum the "miles" field, and then join that on teams somehow.
Changing the table structure is pretty much out, as this is an existing application. This is a LAMP environment, so manipulating PHP arrays after the query is an option if needed.
This should do it:
select t.team_id, t.team_name, t.captains_id, sum(m.miles) as total_miles
from teams t
inner join riders r on r.team_id = t.team_id
inner join miles m on m.rider_id = r.rider_id
group by t.team_id, t.team_name, t.captains_id