Send request GROUP by - mysql

How to group the data so that the sorting is respected?
SELECT tt.id_img,
tt.number_public,
tt.time_post,
tt.id_public
FROM (SELECT
`number_public`,
`id_img`,
`time_post`,
birthday_publics.id_public
FROM birthday_time_post LEFT JOIN birthday_publics
ON birthday_publics.id = birthday_time_post.number_public
WHERE birthday_time_post.time_post IN(
SELECT MIN(time_post)
FROM birthday_time_post
GROUP BY number_public
)
ORDER BY time_post
) tt
Sort by column time_post
Photo request
enter image description here

This should be enough
SELECT
b.`number_public`,
b.`id_img`,
b.`time_post`,
birthday_publics.id_public
FROM birthday_time_post b
INNER JOIN (
SELECT MIN(time_post) min_time
FROM birthday_time_post
GROUP BY number_public
) t on t.min_time = b.time_post
LEFT JOIN birthday_publics ON birthday_publics.id =b.number_public
order by b.time_post

Related

2 requests in only one using mysql

I have two requests
UPDATE :
I need to do something like that :
SELECT poste_nom, ups_type_contrat,
(SELECT `entpro_date`
FROM ENT_PRO
WHERE entpro_user_id = 2
ORDER BY `entpro_id` DESC
LIMIT 1) ,
serv_nom,
serv_id_resp,
user_credit_cpf,
user_indice_salarial,
FLOOR( DATEDIFF( CURDATE( ) , user_dateentree ) /365 ) AS dateEntree
FROM USER
INNER JOIN USER_POSTE_SERVICE
ON USER.user_id= USER_POSTE_SERVICE.ups_poste_id
INNER JOIN POSTE
ON USER_POSTE_SERVICE. ups_poste_id = POSTE.poste_id
INNER JOIN SERVICE
ON USER_POSTE_SERVICE.ups_id_serv = SERVICE.serv_id
WHERE user_id = 2
ORDER BY user_nom ASC
Is it possible to gather two requests in only one ?
From what I understood you want to simple merge the result of your sub-query to your main SELECT, if so you could try it this way:
SELECT poste_nom,
ups_type_contrat,
ENT_PRO_RESULT.entpro_date,
serv_nom,
serv_id_resp,
user_credit_cpf,
user_indice_salarial,
FLOOR( DATEDIFF( CURDATE( ) , user_dateentree ) /365 ) AS dateEntree
FROM USER
LEFT JOIN (SELECT entpro_date,
entpro_user_id
FROM ENT_PRO
ORDER BY entpro_id DESC
LIMIT 1) ENT_PRO_RESULT
ON USER.user_id = ENT_PRO_RESULT.entpro_user_id
INNER JOIN USER_POSTE_SERVICE
ON USER.user_id = USER_POSTE_SERVICE.ups_poste_id
INNER JOIN POSTE
ON USER_POSTE_SERVICE.ups_poste_id = POSTE.poste_id
INNER JOIN SERVICE
ON USER_POSTE_SERVICE.ups_id_serv = SERVICE.serv_id
WHERE user_id = 2
ORDER BY user_nom ASC
I've joined it on:
ON USER.user_id = ENT_PRO_RESULT.entpro_user_id
So you only need to specify the:
WHERE user_id = 2
And the sub-query will use the current row user id for the LEFT JOIN.

Count invitation response against events and response codes

I have this table for Response Codes:
And this table for invitations:
My query so far gives this:
While I want to achieve this:
MY QUERY:
SELECT
i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
LEFT JOIN response_codes code
ON code.responseCode = i.attendeeResponse
GROUP BY i.eventId, code.responseCode, i.attendeeResponse;
SQLFiddle
You need to construct a cartesian product of all eventIds and responseCodes at first (you can achieve it with join without condition):
select c.eventId
, c.responseCode
, count( i.attendeeResponse ) as responseCount
from ( select distinct t1.responseCode
, t2.eventId
from `response_codes` t1
join `invitations` t2 ) c
left join `invitations` i on c.responseCode = i.attendeeResponse and c.eventId = i.eventId
group by c.eventId, c.responseCode;
SQLFiddle
You need to cross join the responsecode table to get the all combinations of eventid and responsecode.
SQL Fiddle
SELECT distinct
i.eventId
,code.responseCode
,case when t.responseCount is null then 0
else t.responsecount end rcount
FROM invitations i
cross JOIN response_codes code
left join
(SELECT i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
JOIN response_codes code
ON code.responseCode = i.attendeeResponse
group by i.eventid, code.responsecode) t
on t.responsecode =code.responsecode and t.eventid = i.eventid
order by i.eventid, code.responsecode desc
Another lazy way could be:
SELECT B.EVENTID,A.RESPONSECODE,
IFNULL((SELECT COUNT(*) FROM INVITATIONS C WHERE C.EVENTID = B.EVENTID AND C.ATTENDEERESPONSE = A.RESPONSECODE),0) AS 'responseCount'
FROM
RESPONSE_CODES A,
INVITATIONS B
GROUP BY A.RESPONSECODE,B.EVENTID
ORDER BY EVENTID ASC,RESPONSECODE DESC
SQL Fiddle

bring the last 10 orders whitout repeating

I have the following query
SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)
ORDER BY b.id_bitacora DESC
LIMIT 10
and it brings me this
as you see the id column of the order is repeated because of the cross with bitacora table i need not to be repeated, any ideas? Thanks in advance.
DISTINCT should do the job in your case, as all the columns' data are repeated for the row not just the id column:
SELECT DISTINCT
a.id, a.fecha, a.ser, a.numero,
c.nombre_apellido, a.estado,
a.tipo, a.articulo, a.precio_asignado,
a.retirada, a.pronta, a.precio,
a.confirmada, d.marca, a.modelo,
a.fecha_prometido, a.fecha_asignado,
a.presupuesto, a.cant_llamados
FROM ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
LEFT JOIN clientes c ON a.cliente_id = c.id
LEFT JOIN marcas d ON a.marca_id = d.id
ORDER BY b.id_bitacora DESC
LIMIT 10
SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
(SELECT DISTINCT id_orden
FROM `ordenes_servicio_bitacora`
ORDER BY id_bitacora DESC
LIMIT 10) b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)
i just made a subquery and the job is done, thanks by the answers and comments :)

Complex mysql query probably build subquery

In my case, I have data from multiple tables that I want to extract a single query. It's about football statistics. In one minute table recorded the dates of the matches and the other recorded data and results of the matches.
The problem is that I want to limit the applications of dates, not the number of matches, as in a day has a few games.
Managed to build a complex query that displays all my data, but it displays the results on the number of games rather than the dates so I can not use limitation, because eating in this case becomes more games rather than dates.
My question is is it possible to build an application that has a limitation on the dates and at the same time to display the results of all matches played in the dates?
Here is the code of the application that I use now:
SELECT
MAIN.id,
SECTION.type,
MAIN.date as date_,
MAIN.prognosis,
HOME_TEAM.team_name as home_team,
GUEST_TEAM.team_name as guest_team,
FIRST_INDEX.index as f_index,
SECOND_INDEX.index as s_index,
THIRD_INDEX.index as t_index,
DATA.home_result,
DATA.guest_result,
DATA.coefficient,
DATA.success,
MAIN.total_coefficient,
MAIN.total_success
FROM ssdt_matches_main as MAIN
LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id )
WHERE SECTION.type = 'Risk prognosis'
ORDER BY MAIN.id DESC
You want to limit the dates in a where clause like:
where MAIN.date between date('2012-01-01') and date('2012-12-31');
If you want to get the records from, say, the most recent 10 days (with a match), you can do something like this:
select . . .
from . . . join
(select date
from ssdt_matches_main md
group by date
order by date desc
limit 10
) datel
on datel.date = MAIN.date
This uses a join to select a list of dates and then a join to do the filtering.
EDIT:
Your from clause would look like:
FROM ssdt_matches_main as MAIN
LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id )
join (select date
from ssdt_matches_main md
group by date
order by date desc
limit 10
) datel
on datel.date = MAIN.date
I fixed the query.
I would not have done it without your help.
Thank you very much!
SELECT
MAIN.id,
SECTION.type,
MAIN.date as date_,
MAIN.prognosis,
HOME_TEAM.team_name as home_team,
GUEST_TEAM.team_name as guest_team,
FIRST_INDEX.index as f_index,
SECOND_INDEX.index as s_index,
THIRD_INDEX.index as t_index,
DATA.home_result,
DATA.guest_result,
DATA.coefficient,
DATA.success,
MAIN.total_coefficient,
MAIN.total_success
FROM ssdt_matches_main as MAIN
LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id )
JOIN (SELECT id
FROM ssdt_matches_main md
WHERE type_id = 2
ORDER BY id DESC
LIMIT 0,5
) datel
ON datel.id = DATA.matches_main_id
ORDER BY MAIN.id DESC

Help me optimize this query. Please

$query_index_neighborhood1 =
"SELECT areas_db.areas_name, areas_db.areas_id, neighborhoods_db.neighborhoods_id,
neighborhoods_db.neighborhoods_name, neighborhoods_db.neighborhoods_area_id,
areas_db.areas_state_id
FROM (
(
(
restaurants_db
INNER JOIN neighborhoods_db ON neighborhoods_db.neighborhoods_id=restaurants_db.restaurants_neighborhood
)
INNER JOIN areas_db ON areas_db.areas_id=neighborhoods_db.neighborhoods_area_id
)
INNER JOIN areas_db AS areas_db1 on areas_db1.areas_id=restaurants_db.restaurants_area
)
WHERE areas_db.areas_state_id=$mxstateid
GROUP BY neighborhoods_db.neighborhoods_id
ORDER BY areas_db.areas_id, neighborhoods_db.neighborhoods_name ASC";
As an interesting thought exercise, I came up with the following:
SELECT a.areas_name,
a.areas_id,
n.neighborhoods_id,
n.neighborhoods_name,
n.neighborhoods_area_id,
a.areas_state_id
FROM neighborhoods_db AS n
INNER JOIN areas_db AS a ON a.areas_id = n.neighborhoods_area_id
WHERE a.areas_state_id = $mxstateid
AND n.neighborhoods_id in (SELECT restaurants_neighborhood FROM restaurants_db)
ORDER BY a.areas_id, n.neighborhoods_name ASC
Also, table aliases are your friend.