i have this script.. first i run query 1 and store into array then query 2,
using a foreach, i combine them and create a list of urls..but this takes time.. is there a way i can do this just in mysql by combining the table even do they have no common column?
query 1
SELECT
c.id,
c.city_name,
r.region_name,
cr.country_name
FROM city AS c, region AS r, country AS cr
WHERE r.id = c.id_region
AND cr.id = c.id_country
AND cr.id IN
(SELECT id FROM country WHERE used = 1)
query 2
SELECT id, title FROM param WHERE active = 1
loop
foreach ($arrayCity as $city) {
foreach ($arrayParam as $param ) {
$paramTitle = str_replace(' ', '+', $param['title']);
$url = 'http://url/city/'. $city['id'] .'/paramId/'. $param['id'] .'/'.
$paramTitle .'/'. $city['region_name'] .'/'. $city['city_name'];
}
}
You don't have to join on a condition. Not doing so is called a CROSS JOIN. The CROSS keyword is optional. In fact, you are already doing this because , is a synonym.
FROM city AS c, region AS r, country AS cr, param
How about
Select 'http://url/city/' + c.id + '/paramId/' + p.id + '/' +
Replace(title, ' ', '+') + '/' + r.region_Name + '/' + c.city_Name
From city c
Join region r On r.id = c.id_region
Join country n On n.id = c.id_country
cross join param p
Where n.Uused = 1
And p.active = 1
Something like this. You can select a concatenated string with results from your base table.
SELECT
c.id,
c.city_name,
r.region_name,
cr.country_name,
('http://url/city/' + c.city_name + '/' + p.param + '/' + c.id) AS URL
FROM city AS c, region AS r, country AS cr
JOIN param p ON c.id = p.id
WHERE r.id = c.id_region AND cr.id = c.id_country AND cr.id IN (SELECT id FROM country WHERE used = 1)
My experience is in TSQL, but it'll be close in my SQL
Edit: Sorry, forgot the join
Related
I need to join 2 queries within a database but place it in 2 different column
i tried union but the result are in the same column "total fulltime"
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as 'Total fulltime'
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='2'
AND sh.staffperiodyear = 'FY2018')
union
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as 'Total parttime'
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='3'
AND sh.staffperiodyear = 'FY2018')
How do i join it so that there are 2 different column('total fulltime' & 'total parttime') with the total sum?
You could use your queries with cross join
select t1.Total_fulltime
, t2.Total_parttime
, t1.Total_fulltime + t2.Total_parttime total_sum
from (SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as Total_fulltime
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='2'
AND sh.staffperiodyear = 'FY2018') t1
CROSS JOIN
(SELECT (SUM(servicehours) + SUM(`teachinghours`) + SUM(`researchhours`)) as Total_parttime
FROM staff_hour sh,staff s,role r
WHERE r.roleid =s.roleid
AND s.staffid = sh.staffid
AND r.roleid='3'
AND sh.staffperiodyear = 'FY2018') t2
So I'm trying to get some total numbers of different tables in the database.
This is the query that I'm using
$this->db->select('
c.*,
SUM(rm.discount_value) as totalDiscount,
SUM(a.status = 0) as totalVisits,
SUM(a.status != 0) as totalAnnulations,
SUM(r.treatment_price + r.arrangement_price + r.product_price) + c.start_rev as totalRevenue
')
->join('customers c','c.customer_id = a.customer_id','left')
->join('revenue r','r.customer_id = a.customer_id','left')
->join('remarks rm','rm.customer_id = a.customer_id','left')
->from('agenda a')
->where('a.user_id',$user_id)
->where('a.customer_id',$customer_id)
->group_by('a.customer_id');
This results in the following query.
SELECT `c`.*,
SUM(rm.discount_value) as totalDiscount,
SUM(a.status = 0) as totalVisits, SUM(a.status != 0) as totalAnnulations,
SUM(r.treatment_price + r.arrangement_price + r.product_price) + c.start_rev as totalRevenue
FROM (`agenda` a)
LEFT JOIN `customers` c ON `c`.`customer_id` = `a`.`customer_id`
LEFT JOIN `revenue` r ON `r`.`customer_id` = `a`.`customer_id`
LEFT JOIN `remarks` rm ON `rm`.`customer_id` = `a`.`customer_id` WHERE `a`.`user_id` = '9' AND `a`.`customer_id` = '4134'
GROUP BY `a`.`customer_id`
This query is returning 4810 visits, but there are only 74.
This returns too many results. Its like its not using that a.customer_id
Any help is welcome, thanks in advance.
You are using GROUP BY with a column that does not appear in the SELECT. See how to use GROUP BY.
Could you please help me to change the script with substring or any other alternative way to improve the query.
I am not a developer it would be great if you can provide a sample script from the below script. the table valued function cost 49% in actual execution plan of the query and runs for 7 minutes.
Thank you in advance.
LEFT OUTER JOIN
(SELECT
pp2.patient_id,
Foodallergy= STUFF((SELECT ',' + cd.description
FROM [OHCP_OHProblemList].[problemlist].[problems_v] pp
INNER JOIN [OHCP_OHCLINICAL].[CodeSet].[CodeSet] CS ON cs.identifier = pp.problem_name_code_set
INNER JOIN ohcp_ohclinical.codeset.CodeDefinition cd ON (cd.code = pp.problem_name_code AND cd.codesetid = cs.id)
WHERE patient_id = pp2.patient_id
AND pp.last_update_action NOT IN ('DELETE', 'CLOSE')
AND pp.adr_class_code IN (3,4)
AND cd.description != 'Other'
ORDER BY [description]
FOR XML PATH ('')), 1, 1, ''),
description = STUFF((SELECT N',' + ' Other:' + pp.problem_name_freetext_desc
FROM [OHCP_OHProblemList].[problemlist].[problems_v] pp
INNER JOIN [OHCP_OHCLINICAL].[CodeSet].[CodeSet] CS on cs.identifier = pp.problem_name_code_set
INNER JOIN ohcp_ohclinical.codeset.CodeDefinition cd ON (cd.code = pp.problem_name_code AND cd.codesetid = cs.id)
WHERE patient_id = pp2.patient_id
AND pp.last_update_action not in ('DELETE', 'CLOSE')
AND pp.adr_class_code in (3, 4)
FOR XML PATH('')), 1, 1, '')
FROM
[OHCP_OHProblemList].[problemlist].[problems_v] pp2
GROUP BY
pp2.patient_id) AS problem ON problem.patient_id = externalpatientid.externalpatientid
I wish to call my function (dbo.Split) with a column table (U.UbicacionF) but it doesn't work! Please can u help me!! Thanks
SELECT
EE.IDEstatus, EE.IDAsignacion,EE.IDUNIDAD,
EE.IDEQUIPO,EE.ESTATUS,EE.bUltEstatus,
A.IDDestino, U.UbicacionF, TI.Tiempo
FROM
mar_EstatusEquipo EE
LEFT JOIN
mar_Asignaciones A ON EE.IDAsignacion = A.IDAsignacion
AND EE.IDUNIDAD = A.IDUNIDAD
LEFT JOIN
mar_TmpUbicaciones U ON EE.IDUNIDAD = U.IdUnidad AND U.IdUnidad = A.IDUNIDAD
LEFT JOIN
mar_TiemposArriboPto TI ON A.IDDestino = TI.Destino
AND TI.Ubicacion LIKE '%' + (SELECT *
FROM dbo.Split(U.UbicacionF, ',', '1')) + '%'
WHERE
EE.IDEquipo = CAST(EE.IDUNIDAD AS CHAR)
AND EE.Estatus IN ('IT','ET')
AND A.IDDestino = 'MZO'
AND EE.bUltEstatus = 1
AND EE.IDUNIDAD = 255
ORDER BY
EE.IDUNIDAD
You could use an APPLY operator to pass a column to a table-valued function like that. In your case it would rather be OUTER APPLY than CROSS APPLY, to match your current outer join logic:
SELECT
EE.IDEstatus, EE.IDAsignacion,EE.IDUNIDAD,
EE.IDEQUIPO,EE.ESTATUS,EE.bUltEstatus,
A.IDDestino, U.UbicacionF, TI.Tiempo
FROM
dbo.mar_EstatusEquipo EE
LEFT JOIN
dbo.mar_Asignaciones A ON EE.IDAsignacion = A.IDAsignacion
AND EE.IDUNIDAD = A.IDUNIDAD
LEFT JOIN
dbo.mar_TmpUbicaciones U ON EE.IDUNIDAD = U.IdUnidad AND U.IdUnidad = A.IDUNIDAD
OUTER APPLY (
SELECT t.Tiempo
FROM
dbo.mar_TiemposArriboPto AS t
INNER JOIN
dbo.Split(U.UbicacionF, ',', '1') AS s ON t.Ubicacion LIKE '%' + s.Value + '%'
) AS TI
WHERE
EE.IDEquipo = CAST(EE.IDUNIDAD AS CHAR)
AND EE.Estatus IN ('IT','ET')
AND A.IDDestino = 'MZO'
AND EE.bUltEstatus = 1
AND EE.IDUNIDAD = 255
ORDER BY
EE.IDUNIDAD
;
i need to add two values from two subqueries and add it to third column.
i can write whole subqueries twice to produce sum, but is there better way to do it?
SELECT
d.id,
CONCAT(d.disease, '( ', d.disease_nepali, ' ) ') AS DISEASE,
IFNULL((SELECT
patients.D_O_M + patients.D_O_F
FROM
patients
WHERE
clinic = 22
AND patients.disease = p.disease),
0) AS 'district1',
IFNULL((SELECT
patients.D_O_M + patients.D_O_F
FROM
patients
WHERE
clinic = 21 AND disease = p.disease),
0) AS 'district2'
FROM
diseases d
LEFT JOIN
patients p ON (d.id = p.disease AND p.district = 9
AND p.status = 1
AND p.report_date LIKE '2014-03%')
GROUP BY disease
You can use a sub select
SELECT t.*,t.district1 + t.district2 `new_col`
FROM (
SELECT d.id, CONCAT(d.disease, '( ' ,d.disease_nepali, ' ) ') AS DISEASE,
IFNULL((SELECT patients.D_O_M+patients.D_O_F FROM patients WHERE clinic = 22 AND patients.disease = p.disease),0) AS `district1` ,
IFNULL((SELECT patients.D_O_M+patients.D_O_F FROM patients WHERE clinic = 21 AND disease = p.disease),0) AS `district2`
FROM diseases d
LEFT JOIN patients p ON
(d.id = p.disease AND p.district = 9 AND p.status = 1 AND p.report_date LIKE '2014-03%')
GROUP BY disease
) t