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
Related
My below query works, but there are two things I want to get from the query that I don't know how to do.
How to tell which LEFT JOIN the final returned row is coming from?
Is it possible to also return the total count from each LEFT JOIN?
SELECT * FROM (
(SELECT ch.user_ID, ch.clID FROM clubHistory AS ch
LEFT OUTER JOIN clubRaffleWinners AS cr ON
ch.user_ID = cr.user_ID
AND cr.cID=1157
AND cr.rafID=18
AND cr.chDate1 = '2022-06-04'
WHERE ch.cID=1157
AND ch.crID=1001
AND ch.ceID=1167
AND ch.chDate = '2022-06-04'
AND cr.user_ID IS NULL
GROUP BY ch.user_ID )
UNION ALL
(SELECT cu.user_ID, cu.clID FROM clubUsers AS cu
LEFT OUTER JOIN clubRaffleWinners AS cr1 ON
cu.user_ID = cr1.user_ID
AND cr1.cID=1157
AND cr1.rafID=18
AND cr1.chDate1 = '2022-06-04'
WHERE cu.cID=1157
AND cu.crID=1001
AND cu.ceID=1167
AND cu.calDate = '2022-06-04'
AND cr1.user_ID IS NULL
GROUP BY cu.user_ID )
) as winner ORDER BY RAND() LIMIT 1 ;
In my two left join select statements I tried:
(SELECT ch.user_ID as chUserID, ch.clID FROM clubHistory AS ch
and
(SELECT cu.user_ID as cuUserID, cu.clID FROM clubUsers AS cu
But every single result, after dozens and dozens of tries comes back a user_ID or chUserID. When I remove the ORDER BY RAND() LIMIT 1 - the only two columns that come back are user_ID, clID or chUserID, clID even though the combined results is the full list of both tables. Is this even possible?
And #2 above, is it possible to extract the total counts from each LEFT JOIN with and with out the final order by rand() limit 1 ???
For 1 add an extra column containing a value that identifies which subquery of the UNION it is.
SELECT * FROM (
(SELECT 'history' AS which, ch.user_ID, ch.clID FROM clubHistory AS ch
LEFT OUTER JOIN clubRaffleWinners AS cr ON
ch.user_ID = cr.user_ID
AND cr.cID=1157
AND cr.rafID=18
AND cr.chDate1 = '2022-06-04'
WHERE ch.cID=1157
AND ch.crID=1001
AND ch.ceID=1167
AND ch.chDate = '2022-06-04'
AND cr.user_ID IS NULL
GROUP BY ch.user_ID )
UNION ALL
(SELECT 'users' AS which, cu.user_ID, cu.clID FROM clubUsers AS cu
LEFT OUTER JOIN clubRaffleWinners AS cr1 ON
cu.user_ID = cr1.user_ID
AND cr1.cID=1157
AND cr1.rafID=18
AND cr1.chDate1 = '2022-06-04'
WHERE cu.cID=1157
AND cu.crID=1001
AND cu.ceID=1167
AND cu.calDate = '2022-06-04'
AND cr1.user_ID IS NULL
GROUP BY cu.user_ID )
) as winner ORDER BY RAND() LIMIT 1 ;
Please only ask one question at a time.
i am Using This Mysql Query and is working Good, But i need go get a Better Time result. How can i do it?
SELECT TblExistencias.id as ID, TblExistencias.codigo as Codigo,
TblPartes.detalle as Detalle,TblPartes.neto1 as PrecioActual,
TblExistencias.Condicion_Producto as Condicion,TblCategorias.categoria as Categoria,
TblSubcategorias.subcategoria as Subcategoria, TblExistencias.costo as Costo,
TblExistencias.serial as Serial, TblExistencias.vendido as Vendido,
TblConceptosFacturas.ventaonline as VentaOnline, TblRemitos.nroremitocompleto as Remito,
TblFacturas.nrofacturacompleto as Factura, TblFacturas.fecha as FechaVenta,
TblConceptosFacturas.ventaTotUn as Venta,TblConceptosFacturas.comisionmlunit as Comision,
TblFacturas.costoenvio as Envio, if(TblExistencias.vendido =1,
TblConceptosFacturas.ventaTotUn - TblExistencias.Costo - TblConceptosFacturas.comisionmlunit - TblFacturas.costoenvio,0) as Ganancia,
TblProveedores.razonsocial as Proveedor, TblFacturasCompras.nrofacturacompleto as Compra,
TblFacturasCompras.fecha as FechaCompra, TblClientes.razonsocial as Cliente
from TblExistencias
left join TblPartes on TblExistencias.codigo = TblPartes.codigo1
left join TblRemitos on TblExistencias.id_RemitoVenta = TblRemitos.id
left join TblFacturasCompras on TblExistencias.id_factura = TblFacturasCompras.id
left join TblClientes on TblRemitos.id_cliente = TblClientes.id
left join TblFacturas on TblRemitos.id_factura = TblFacturas.id
left join TblConceptosFacturas on TblFacturas.id=TblConceptosFacturas.id_factura and TblConceptosFacturas.codigoproducto = TblExistencias.codigo
left join TblCategorias on TblCategorias.id = TblPartes.id_categoria
left join TblSubcategorias on TblPartes.id_subcategoria = TblSubcategorias.id
left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id
order by comision desc
The solution is create an index on each relational columns, like:
TblFacturasCompras.id_proveedor = TblProveedores.id
Problem was in order clause, Query passed from 87 Secs to 4 Secs. Thanks for help.
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
How can I adjust this JOIN clause so that rows with a NULL value for the CountLocId or CountNatId columns are returned in the result?
In other words, if there is no match in the local_ads table, I still want the user's result from the nat_ads table to be returned -- and vice-versa.
SELECT u.franchise, CountLocId, TotalPrice, CountNatId, TotalNMoney, (
TotalPrice + TotalNMoney
)TotalRev
FROM users u
LEFT JOIN local_rev lr ON u.user_id = lr.user_id
LEFT JOIN (
SELECT lrr_id, COUNT( lad_id ) CountLocId, SUM( price ) TotalPrice
FROM local_ads
GROUP BY lrr_id
)la ON lr.lrr_id = la.lrr_id
LEFT JOIN nat_rev nr ON u.user_id = nr.user_id
INNER JOIN (
SELECT nrr_id, COUNT( nad_id ) CountNatId, SUM( tmoney ) TotalNMoney
FROM nat_ads
WHERE MONTH = 'April'
GROUP BY nrr_id
)na ON nr.nrr_id = na.nrr_id
WHERE lr.month = 'April'
AND franchise != 'Corporate'
ORDER BY franchise
Thanks in advance for your help!
try the following in where clause while making a left join. This will take all rows from right table with matched condition
eg.
LEFT JOIN local_rev lr ON (u.user_id = lr.user_id) or (u.user_id IS NULL)
Use this template, as it ensures that :
you have only one record per user_id (notice all subquerys have a GROUP BY user_id) so for one record on user table you have one (or none) record on subquery
independent joins (and calculated data) are not messed togeder
-
SELECT u.franchise, one.CountLocId, one.TotalPrice, two.CountNatId, two.TotalNMoney, (COALESCE(one.TotalPrice,0) + COALESCE(two.TotalNMoney,0)) TotalRev
FROM users u
LEFT JOIN (
SELECT x.user_id, sum(xORy.whatever) as TotalPrice, count(xORy.whatever) as CountLocId
FROM x -- where x is local_rev or local_ads I dont know
LEFT JOIN y on x.... = y.... -- where y is local_rev or local_ads I dont know
GROUP BY x.user_id
) as one on u.user_id = one.user_id
LEFT JOIN (
SELECT x.user_id, sum(xORy.whatever) as TotalNMoney, count(xORy.whatever) as CountNatId
FROM x -- where x is nat_rev or nat_ads I dont know
LEFT JOIN y on x.... = y.... -- where y is nat_rev or nat_ads I dont know
GROUP BY x.user_id
) as two on u.user_id = two.user_id
I've been at this for a bit now. Basically, I'm needing to add a derived column to count the hits to a weblog entry in the database. The problem is, the hits are being totaled and shown on only on the first record. Any Ideas? I've emboldened the parts of the query I'm talking about. The query is below:
SELECT DISTINCT(t.entry_id),
exp_categories.rank,
**exp_hits.hits,**
t.entry_id,
t.weblog_id,
t.forum_topic_id,
t.author_id,
t.ip_address,
t.title,
t.url_title,
t.status,
t.dst_enabled,
t.view_count_one,
t.view_count_two,
t.view_count_three,
t.view_count_four,
t.allow_comments,
t.comment_expiration_date,
t.allow_trackbacks,
t.sticky,
t.entry_date,
t.year,
t.month,
t.day,
t.entry_date,
t.edit_date,
t.expiration_date,
t.recent_comment_date,
t.comment_total,
t.trackback_total,
t.sent_trackbacks,
t.recent_trackback_date,
t.site_id as entry_site_id,
w.blog_title,
w.blog_name,
w.search_results_url,
w.search_excerpt,
w.blog_url,
w.comment_url,
w.tb_return_url,
w.comment_moderate,
w.weblog_html_formatting,
w.weblog_allow_img_urls,
w.weblog_auto_link_urls,
w.enable_trackbacks,
w.trackback_use_url_title,
w.trackback_field,
w.trackback_use_captcha,
w.trackback_system_enabled,
m.username,
m.email,
m.url,
m.screen_name,
m.location,
m.occupation,
m.interests,
m.aol_im,
m.yahoo_im,
m.msn_im,
m.icq,
m.signature,
m.sig_img_filename,
m.sig_img_width,
m.sig_img_height,
m.avatar_filename,
m.avatar_width,
m.avatar_height,
m.photo_filename,
m.photo_width,
m.photo_height,
m.group_id,
m.member_id,
m.bday_d,
m.bday_m,
m.bday_y,
m.bio,
md.*,
wd.*
FROM exp_weblog_titles AS t
LEFT JOIN exp_weblogs AS w ON t.weblog_id = w.weblog_id
LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id
LEFT JOIN exp_members AS m ON m.member_id = t.author_id
LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id
LEFT JOIN exp_category_posts ON wd.entry_id = exp_category_posts.entry_id
**LEFT JOIN
(
SELECT COUNT(*) AS hits, exp_hits.entry_id FROM exp_hits ORDER BY exp_hits.entry_id
) exp_hits ON t.entry_id = exp_hits.entry_id**
LEFT JOIN
(
SELECT exp_categories.cat_id, cat_name as rank
FROM exp_categories
WHERE exp_categories.group_id = '9'
) exp_categories ON exp_categories.cat_id = exp_category_posts.cat_id
WHERE t.entry_id IN (2,3,4) ORDER BY exp_categories.rank DESC, **exp_hits.hits DESC**, entry_date desc
Try changeing the subselect to
SELECT COUNT(*) AS hits,
exp_hits.entry_id
FROM exp_hits
GROUP BY exp_hits.entry_id
Out of curiosity, is your hits functionality something that can't be accomplished with the view_count_one/two/three/four fields already present in the database and supported by ExpressionEngine template tags?