I'm trying to join 6 tables and want to display payperiod_sdesc, gross_pay, esi_employer_contribution, pf_employer_contribution and employerContribution.
I tried it from my end but difficulty in joining . I tried with four table join but remaining two table I find it difficult
left join
py_process_labour_welfare
on
py_pay_group.pg_code = py_process_labour_welfare.pg_code
this line of join is not working ......
select payperiod_sdesc,gross_pay,esi_employer_contribution,employerContribution
from py_process_emp_status_approved
left join py_process_tds_pf_pt
on py_process_emp_status_approved.ou_code = py_process_tds_pf_pt.ou_code
left join py_pay_group
on py_process_tds_pf_pt.pg_code = py_pay_group.pg_code
left join py_process_labour_welfare
on py_pay_group.pg_code = py_process_labour_welfare.pg_code
left join py_pay_period
on py_process_labour_welfare.payperiod_code = py_pay_period.payperiod_code
left join py_payroll_calendar
on py_pay_period.paycal_code = py_payroll_calendar.paycal_code
I have attached all six table image links.
Related
I'm using this query on mysql:
SELECT
`sitio`.`id_unico_sitio` AS 'ID Unico',
`sitio`.`nombre` AS 'Nombre',
`departamentos`.`departamento` AS 'Departamento',
`municipios`.`municipio` AS 'Municipio',
`sitio`.`direccion` AS 'Direccion',
`sitio`.`coor_x` AS 'Latitud',
`sitio`.`coor_y` AS 'Longitud',
`operador`.`nombre` AS 'Operador',
`tipo_tec`.`tecnologia` AS 'Tecnologia',
`sitio`.`ancho_banda` AS 'Ancho Banda(Mb/s)',
`sitio`.`fecha_inst` AS 'Fecha Instalacion',
`sitio`.`cod_sace` AS 'Cod SACE',
`sitio`.`cod_cnt` AS 'Cod CONATEL'
FROM ((((((((`sitio`
INNER JOIN `tipo_tec_sitio`
ON `sitio`.`id_unico_sitio` = `tipo_tec_sitio`.`id_unico_sitio`)
INNER JOIN `tipo_tec`
ON `tipo_tec_sitio`.`id_tipo_tec` = `tipo_tec`.`id_tipo_tec`)
INNER JOIN `administracion`
ON `sitio`.`id_administracion` = `administracion`.`id_administracion`)
INNER JOIN `estatus_finalizado`
ON `sitio`.`id_estatus_finalizado` = `estatus_finalizado`.`id_estatus_finalizado`)
INNER JOIN `departamentos`
ON `sitio`.`id_departamento` = `departamentos`.`id_departamento`)
INNER JOIN `municipios`
ON `sitio`.`id_municipio` = `municipios`.`id_municipio`)
INNER JOIN `sitios_has_operador`
ON `sitio`.`id_unico_sitio` = `sitios_has_operador`.`id_unico_sitio`)
INNER JOIN `operador`
ON `sitios_has_operador`.`id_operador` = `operador`.`id_operador`)
The problem is that with that query I'm only getting the first 2 rows when on the database there are five rows.
Is there something wrong or something that limits the number of rows returned from the database on that query?
Since you are using INNER JOIN for all the tables, are you sure you have all five rows inside all the tables ? if one of the table only contains two rows, then at the end it will only show two rows.
Make sure that your INNER JOIN condition matches all the five rows in the table you need.
create or replace view vw_fill_thresholds as
SELECT
fill_thresholds.id,
fill_thresholds.tenant_id,
waypoint_locations.name,
material_type.description as material,
fill_thresholds.can_size,
fill_thresholds.threshold,
units.description as unit,
fill_thresholds.post_threshold_cost as ptc,
ptu_units.description as ptu_unit
FROM fill_thresholds
left join fill_locations on fill_thresholds.fill_id = fill_locations.id
and fill_thresholds.deleted = 0
left join units on fill_locations.unit_id = units.unit_id
left join waypoint_locations on `waypoint_locations`.`id` = `fill_locations`.`waypoint_id`
left join `material_type` on `material_type`.`id` = `fill_locations`.`material`
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = units.unit_id
I need to select the same column as units. So, In my fill_thresholds table I have two fields that contain an id from the units table: example 2 and 3.
Now, in my units table I have the following fields: id and description. example data: 1 km, 2 miles, 3 yards ,etc. so in my view according to the numbers I have in fill_thresholds it should show miles and yards. Right now I'm getting NULLs which I don't understand why.
Your last join should be as per below-
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = ptu_units.unit_id
Right now you are joining your units table based on 2 columns first with fill_locations.unit_id = units.unit_id and 2nd with fill_thresholds.post_threshold_unit_id = units.unit_id, so not getting corresponding rows ans showing null.
The problem with your query is that you're trying to guess what is wrong analysing the whole query. It is too complicated so try to join your tables one by one.
1)fill_thresholds LEFT JOIN fill_locations
2)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units
3)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units LEFT JOIN waypoint_locations
and so on
This way you can find where is the problem
I am actually trying to Join Five Tables in MySQL Database based upon RegionID which is a unique key present in all tables. I am new to it and using the following left join statement:
SELECT `tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*,
`tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*
FROM tbllistings
LEFT JOIN `webspace_db`.`tblregions`
ON `tbllistings`.`intregionid` = `tblregions`.`intregionid`
LEFT JOIN `webspace_db`.`tblcontent`
ON `tblregions`.`intregionid` = `tblcontent`.`intregionid`
LEFT JOIN `webspace_db`.`tblgallery`
ON `tblregions`.`intregionid` = `tblgallery`.`regionid`
LEFT JOIN `webspace_db`.`tbltowns`
ON `tblregions`.`intregionid` = `tbltowns`.`intregionid`
The problem that I am facing is each value is showing up more then 20+ times and I am unsure why is this happening. What I actually want is to simply join all fields from all tables based on RegionID.
Any help and suggestions are highly welcomed. Thanks a lot.
Use tbllistings.intregionid in every ON clause.
SELECT `tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*,
`tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*
FROM tbllistings
LEFT JOIN `webspace_db`.`tblregions`
ON `tblregions`.`intregionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tblcontent`
ON `tblcontent`.`intregionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tblgallery`
ON `tblgallery`.`regionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tbltowns`
ON `tbltowns`.`intregionid` = `tbllistings`.`intregionid`
I have this query with many left joins and a inner join with dates.
I need to group by id_art (from articles_art) and date_dat (dates_dat). The problem is that is really slow. it takes 3second for 1000records.
dates_dat is indexed in dates_dat table and id_art is a primary key of articles_art.
What can I do to optimize this query?
SELECT
id_art, image2_art, video_art, website,
text.title_int, text.intro_int, text.text_int, text.extra_int,
dat.date_dat, dat.date2_dat,
group_concat(tim.time_tim),
prd.name_prd,
group_concat(cat.name_cat),
trg.name_trg,
spa.name_spa,
spa2.name_spa
FROM
articles_art AS art
LEFT JOIN internText_int AS text ON text.idart_int = art.id_art
INNER JOIN dates_dat AS dat ON art.id_art = dat.idart_dat
LEFT JOIN spaces_spa As spa ON spa.id_spa = dat.idspa_dat
LEFT JOIN spaces_spa As spa2 ON spa.id_spa = dat.idspa2_dat
LEFT JOIN times_tim AS tim ON tim.iddat_tim = dat.id_dat
LEFT JOIN articles_products_artprd AS artprd ON artprd.idart_artprd = art.id_art
LEFT JOIN products_prd AS prd ON prd.id_prd = artprd.idprd_artprd
LEFT JOIN cater_cev AS cev ON cev.idart_cev = dat.idart_dat
LEFT JOIN categories_cat AS cat ON cat.id_cat = cev.idcat_cev
LEFT JOIN targets_trg AS trg ON trg.id_trg = art.idtrg_art
WHERE
prd.id_prd in (1,2)
AND validated_art = 1
AND text.idlin_int in (1,4)
GROUP BY
id_art, date_dat
Look like you can put an index on these columns
prd.id_prd
validated_art
text.idlin_int
Test this first then if this does not work put indexes on column conditions on the ON clause
If data latency isn't an issue, can you hive the data off (perhaps overnight?) into a single normalised table? That way you query a single table without all those JOINS. You could even apply indexes to help speed things up further.
I have a situation where a property table holds an address id (from the g_addresses table) and an applicant table also holds an address id from the g_addresses.
I'd like to left join these together but select all the fields in the table.
I know of using 'as' to make an alias for fields, but is there any way to produce an alias for a whole table?
SELECT *
FROM (`reference`)
LEFT JOIN `applicants` ON `applicants`.`id` = `reference`.`applicant_id`
LEFT JOIN `g_people` applicant_person ON `applicant_person`.`id` = `applicants`.`person_id`
LEFT JOIN `g_addresses` applicant_address ON `applicant_address`.`id` = `applicants`.`address_id`
LEFT JOIN `properties` ON `properties`.`id` = `reference`.`property_id`
LEFT JOIN `g_addresses` property_address ON `property_address`.`id` = `properties`.`address_id`
WHERE `reference`.`id` = 4
This produces a result containing only one address row and not both,
The row that is returned is the row from the final join and not the one previously, indicating it is overwriting when it is returned.
I don't think you should use masked references, like * or `reference`.*, in your case, because you may end up with a row set containing identical column names (id, address_id).
If you want to pull all the columns from the joined tables, you should probably specify them individually in the SELECT clause and assign a unique alias to every one of them:
SELECT
ref.`id` AS ref_id,
ref.`…` AS …,
…
app.`id` AS app_id,
…
FROM `reference` AS ref
LEFT JOIN `applicants` AS app ON app.`id` = ref.`applicant_id`
LEFT JOIN `g_people` AS ape ON ape.`id` = app.`person_id`
LEFT JOIN `g_addresses` AS apa ON apa.`id` = app.`address_id`
LEFT JOIN `properties` AS pro ON pro.`id` = ref.`property_id`
LEFT JOIN `g_addresses` AS pra ON pra.`id` = pro.`address_id`
WHERE ref.`id` = 4
Be more specific about columns you select
SELECT
applicant_address.*,
property_address.*,
applicants.*,
applicant_person.*,
properties.*
FROM (`reference`)
LEFT JOIN `applicants` ON `applicants`.`id` = `reference`.`applicant_id`
LEFT JOIN `g_people` applicant_person ON `applicant_person`.`id` = `applicants`.`person_id`
LEFT JOIN `g_addresses` applicant_address ON `applicant_address`.`id` = `applicants`.`address_id`
LEFT JOIN `properties` ON `properties`.`id` = `reference`.`property_id`
LEFT JOIN `g_addresses` property_address ON `property_address`.`id` = `properties`.`address_id`
WHERE `reference`.`id` = 4