Left join repeats same value of a column in result - mysql

I have this sql query :
SELECT t1.id_action, DATE_FORMAT(t1.date_creation,'%d/%m/%Y à %H:%i') AS creation_action,
u.id_utilisateur, p.genre AS genre_utilisateur,
p.nom, av.titre_avancement, b.nom_banque, c.nom_courtier
FROM action AS t1
LEFT JOIN type
ON type.id_type=t1.id_type
LEFT JOIN avancement AS av
ON av.id_avancement=t1.id_avancement
LEFT JOIN utilisateur AS u
ON u.id_utilisateur=t1.id_utilisateur
LEFT JOIN personne AS p
ON p.id_personne=u.id_personne
left join offre_concurrente o
on o.id_dossier = t1.id_dossier
left join banque b
on b.id_banque = o.id_banque
left join courtier_concurrent c
on c.id_courtier_concurrent=o.id_courtier_concurrent
WHERE t1.id_dossier=11 AND t1.fait='1'
GROUP BY t1.id_action
ORDER BY t1.date_creation DESC
And in "b.nom_banque" and "c.nom_courtier" it displays null all the other columns are okay except these 2 i can't figure out the exact problem

You should add all the non-aggregated columns in the select to the group by clause, but I can see you are not using any aggregate function so use DISTINCT instead. Try this:
SELECT DISTINCT t1.id_action, DATE_FORMAT(t1.date_creation,'%d/%m/%Y à %H:%i') AS creation_action,
u.id_utilisateur, p.genre AS genre_utilisateur,
p.nom, av.titre_avancement, b.nom_banque, c.nom_courtier
FROM action AS t1
LEFT JOIN type
ON type.id_type=t1.id_type
LEFT JOIN avancement AS av
ON av.id_avancement=t1.id_avancement
LEFT JOIN utilisateur AS u
ON u.id_utilisateur=t1.id_utilisateur
LEFT JOIN personne AS p
ON p.id_personne=u.id_personne
left join offre_concurrente o
on o.id_dossier = t1.id_dossier
left join banque b
on b.id_banque = o.id_banque
left join courtier_concurrent c
on c.id_courtier_concurrent=o.id_courtier_concurrent
WHERE t1.id_dossier=11 AND t1.fait='1'
ORDER BY t1.date_creation DESC

Related

Why the output values are repeating? what should be the right query?

This is the query i wrote, but when i execute the query the values are repeating. so help me to write the right query
SELECT p.id,
p.NAME,
p.year,
p.address,
p.caste,
p.landextent,
p.adharno,
p.drillingdate,
p.pumpseterectiondate,
p.pumpsethp,
p.surveyno,
p.registrationdateinescom,
p.ymdmsdpaid,
p.ymdpaiddate,
p.energisationno,
p.energisationdate,
p.mobile,
p.remarks,
c.constituency_name constituency,
t.NAME taluka,
e.NAME escom,
d.district_name district,
division.divison_name division,
p.crsubmitted,
p.uniqueid,
p.yearofdrilling,
p.yearofpumpset,
p.yearofregistration,
p.yearofenergisation,
p.escomdivuseractive
FROM progress p
INNER JOIN constituency c
ON p.constituency_id = c.id
INNER JOIN taluka t
ON c.taluka_id = t.id
INNER JOIN district d
ON t.district_id = d.id
INNER JOIN divison di
ON d.divison_id = di.id
INNER JOIN divisons division
ON d.divisons_id = division.id
INNER JOIN escomdivison e
ON e.district_id = d.id
WHERE di.id = 3;
I think you need to remove the line
INNER JOIN divisons division on d.divisons_id=division.id
since you already have division table written on the one line above for the INNER JOIN conditions, and those(alias and table) are confused among them.

how do create a where clause to return if any of the columns in where cluase have active=1

I have an SQL query like below which has a where clause as WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1,issue right now is that one or all of pg.active tcg.active ,tpg.active is set to 1 at any time,in the case where one of these is not set to 1 query doesn't return anything.
How do I change the where clause to return if any of pg.active tcg.active ,tpg.active is set to 1?
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE pg.active=1 AND tcg.active=1 AND tpg.active=1
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name
I believe you need to encapsulate the OR part of the WHERE statement in round brackets so that it returns a TRUE on any OR match along with your other conditions.
SELECT
..........
FROM software_products_software_images spsi
INNER JOIN software_images si ON si.software_image_id = spsi.software_image_id
INNER JOIN software_products sp ON sp.id = spsi.software_product_id
LEFT JOIN software_products_software_images_testplan_gate tpg ON tpg.software_products_software_images_id = spsi.Id
LEFT JOIN test_suites tp ON tp.id = tpg.testplan_id
LEFT JOIN software_products_software_images_testcase_gate tcg ON tcg.software_products_software_images_id = spsi.Id
LEFT JOIN test_cases tc ON tc.id = tcg.testcase_id
LEFT JOIN test_suites ts ON ts.id = tc.test_suite_id
LEFT JOIN software_products_software_images_percentage_gate pg ON pg.software_products_software_images_id = spsi.Id
...........................
WHERE (pg.active=1 OR tcg.active=1 OR tpg.active=1)
AND si.software_image='NHSS.QSDK.7.0.1' ORDER BY si.software_image, ts.suite_name

REGEX starts by Subquery

This is a query that i just made and it works correctly
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,n.nit_ID
from lineaasiento l left join clientesam c on c.Dk=l.clientesmayor_OID
left join proveedores p on p.Dk=l.proveedor_OID left join empleado e on e.Dk=l.empleado_OID
left join banco b on b.Dk=l.banco_OID left join cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join nit n on n.Dk=coalesce(c.nit_OID,p.nit_OID,e.nit_OID,b.nit_OID,null)
where (timefcontable between '2013-01-01' and '2013-12-31') and
estadolinea_OID in (Select Dk from estadoasiento where codigo!=4) and
cc.cuentacontable_ID REGEXP '^5'
group by n.Dk
But, i have a problem right now... I need REGEX '^5' be a subquery sentence like this
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,n.nit_ID
from lineaasiento l left join clientesam c on c.Dk=l.clientesmayor_OID
left join proveedores p on p.Dk=l.proveedor_OID left join empleado e on e.Dk=l.empleado_OID
left join banco b on b.Dk=l.banco_OID left join cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join nit n on n.Dk=coalesce(c.nit_OID,p.nit_OID,e.nit_OID,b.nit_OID,null)
where (timefcontable between '2013-01-01' and '2013-12-31') and
estadolinea_OID in (Select Dk from estadoasiento where codigo!=4) and
cc.cuentacontable_ID REGEXP (^a query that returns a lot of values and i need to get the elements that starts by this)
group by n.Dk
It is possible?
For building your regex...
select
'^(' || group_concat(my_value separator '|') || ')' as regex
from
my_table
NOTA: There is a 1024 bytes limit on results returned by group_concat. So if you expect larger results, run the following query before your final query (see below).
set group_concat_max_len=2048
Final query
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,
n.nit_ID
from
lineaasiento l
left join
clientesam c on c.Dk=l.clientesmayor_OID
left join
proveedores p on p.Dk=l.proveedor_OID
left join
empleado e on e.Dk=l.empleado_OID
left join
banco b on b.Dk=l.banco_OID
left join
cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join
nit n on n.Dk=coalesce(c.nit_OID, p.nit_OID, e.nit_OID, b.nit_OID, null)
inner join
(select '^(' || group_concat(my_value separator '|') || ')' as regex from my_table) values
where
(
timefcontable between '2013-01-01' and '2013-12-31'
)
and estadolinea_OID in (
Select
Dk
from
estadoasiento
where
codigo!=4
)
and cc.cuentacontable_ID REGEXP values.regex
group by
n.Dk

mysql get X(point) along with join

I am trying to run a select query joining multiple tables. Ont of the tables has got a column coordinate with point type. Everything works correctly but in joins it does not allow me to select X(coordinate) or X(point(coordinate)).
I can select coordinate in the join and can select X(coordinate) directly on the table but both together dont work.
select x(coordinate) from location_coordinate
The above one works
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.coordinate,
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
This also works. But if i try to do
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.X(coordinate),
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
It does not select and gives me an error saying X is not a column.
Please help
It should be X(ca_coo.coordinate) not ca_coo.X(coordinate).

How to use a case statement to determine which table to left join

How to use a case statement to determine which table to left join to this sql sentence:
SELECT c.dentist_id,
c.pack_id,
c.check_id,
sql1.vendor_no ,
p.pack_trans_id,
r.lot_no,
IF(ml.payment_type='Prepay','Pre-Paying','Post-Paying') payment_type,
ml.image_path,
ml.metal_id,
tp.total_paid_refiner,
sql1.percentage,
sql1.contact_percentage,
sql1.agent_percentage,
c.agent_dentist,
c.check_amt,
c.check_image,
c.check_date_sent,
c.check_no,
c.check_clear_date,
ml.spot_gold_price,
ml.total_weight,
ml.estimated_price,
ml.expect_more,
ml.how_long_acquire,
sa.full_name,
pd.company_name,
pd.phone_no,
pd.email
FROM tbl_check AS c
LEFT JOIN tbl_pack_list AS p ON p.pack_id=c.pack_id
LEFT JOIN tbl_pack_list_details AS pd ON pd.pack_id=c.pack_id
LEFT JOIN
(SELECT vendor_no,
paying_contact,
contact_percentage,
percentage,
pay_to,
agent_percentage,
dentist_id did2,
sales_agent_id
FROM tbl_mst_dentist
GROUP BY dentist_id) sql1 ON sql1.did2=c.dentist_id
LEFT JOIN tbl_refining r ON r.pack_id=c.pack_id
LEFT JOIN tbl_metals_list ml ON ml.pack_id=c.pack_id
LEFT JOIN tbl_payment tp ON tp.pack_id=c.pack_id
LEFT JOIN tbl_sales_agent sa ON sa.sales_agent_id=sql1.sales_agent_id
LEFT JOIN
(SELECT * CASE c.agent_dentist WHEN 'a' THEN
(SELECT sa.* ,
sm.state_code
FROM tbl_sales_agent AS sa,
tbl_mst_state AS sm
WHERE sa.sales_agent_id = c.sale_agent_id
AND sa.state=sm.state_name) ELSE
(SELECT *
FROM tbl_mst_dentist
WHERE dentist_id = c.dentist_id))
WHERE 1
AND c.paying_percent !=0
ORDER BY p.pack_trans_id DESC