I am trying to create an inner-join MySQL Query of two tables:
'swatchset'
->swatchset_id
->swatchset_name
'swatches'
->swatch_id
->swatch_name
->swatch_hex
->etc...
I currently have one record in the 'swatchset' table:
swatchset_id | swatchset_name
8 default
The 'swatches' table is empty.
I want to get as result the 'swatchset'.'swatchset_name'
I guess I don't understand INNER JOIN very well because this Query results nothing:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
INNER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
How can I get at least the found 'swatchset' row as result?
Left outer join will fill with nulls columns of second table when not found:
SELECT `swatchset`.`swatchset_name`, `swatches`.`swatch_id`, `swatches`.`swatch_name`, `swatches`.`swatch_hex`, `swatches`.`swatch_type` FROM `swatchset`
LEFT OUTER JOIN `swatches`
ON `swatches`.`f_swatchset_id` = `swatchset`.`swatchset_id`
WHERE `swatchset`.`swatchset_id` = '8';
inner join doesn't return entries from first table without matching ones in second table
Related
transaction_jobs table
transaction_lines table
SQL executed :
SELECT
tj.id AS tj_id, tj.debit AS tj_debit, tj.credit AS tj_credit, job_id AS tj_job_id,
tl.id AS tl_id, tl.debit AS tl_debit, tl.credit AS tl_credit
FROM transaction_jobs tj
LEFT JOIN transaction_lines tl ON tl.account_id = tj.account_id AND tl.transaction_id = tj.transaction_id
WHERE tj.transaction_id = 198044 AND tj.account_id = 64375
Executed code result:
Result I expected:
Is there a way I could get the result I expected?
You have multiple records of the same transaction on both tables and are matching multiple times.
TJ ID 385610 will match once with TL ID 2277 and again with TL ID 2278. because both have transaction_id 198044.
If you wish for the results to be only one match, you need to use
GROUP BY tj_transaction_id
You can read more here:
https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
I have created a MySQL view to grab data from four differents tables :
CREATE VIEW `documents_scannes_view` AS
select `ds`.`id_document` AS `id_document`,
`ba`.`num_boite` AS `num_boite`,
`ds`.`annee` AS `annee`,
`ds`.`ref_document` AS `ref_document`,
`ds`.`num_client` AS `num_client`,
`cl`.`nom_client` AS `nom_client`,
`ds`.`nbre_a4` AS `nbre_a4`,
`ds`.`nbre_a3` AS `nbre_a3`,
`ds`.`nbre_autres` AS `nbre_autres`,
((`ds`.`nbre_autres` + `ds`.`nbre_a3`) + `ds`.`nbre_a4`) AS `nbre_total`,
`ds`.`date_scan` AS `date_scan`,`ds`.`qualite` AS `qualite`,
`ds`.`id_operateur` AS `id_operateur`,
`ut`.`nom_complet` AS `nom_complet`,
`ds`.`observations` AS `observations`,
`ds`.`chemin` AS `chemin`
from (((`boite_archive` `ba` left join `documents_scannes` `ds` on((`ds`.`num_boite` = `ba`.`num_boite`)))
left join `clients` `cl` on((`ds`.`num_client` = `cl`.`num_client`)))
left join `utilisateurs` `ut` on((`ds`.`id_operateur` = `ut`.`id_user`)))
The tables are respectively :
documents_scannes : List of scanned documents
boite_archive : List of box that contains the scanned documents
clients : List of customer that issued the scanned documents
utilisateurs : List of personal that are scanning the documents
The joins is performed on the primary keys of the tables.
My objective is to get the list of the scanned documents (documents_scannes) from each box (boite_archive) fetching in the same time the name of the client (from clients table) and the name of the operator who has done the scan (from table utilisateurs).
My problem is when I :
SELECT * FROM documents_scannes_view WHERE num_boite = '1131';
The box number 1131 is empty it contains no scanned documents but I get one row :
Who can help me point the source of my error and how I can correct it ?
Thanks in advance.
I am not sure about the joins in the bottom of the query. It's the source of the issue. How I can fix it to get no results when the box is emprt ?
If you don't want select an empty row where the join columns don't match change to an inner join instead of left join
from (((`boite_archive` `ba` inner join `documents_scannes` `ds` on((`ds`.`num_boite` = `ba`.`num_boite`)))
left join `clients` `cl` on((`ds`.`num_client` = `cl`.`num_client`)))
left join `utilisateurs` `ut` on((`ds`.`id_operateur` = `ut`.`id_user`)))
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
Image ref.
I have three tables: nanoProd, nanoFiles, and nanoRelFiles. Third table is used to store the file meta and how they relate to other screens.
I'm getting an error that a column doesn't exist when I know it does and I'm not sure why:
no such column: nanoFiles.fileLoc:
SELECT
prodTable.name AS prodName,
prodTable.intro AS prodIntro,
prodTable.prodText AS nanoText,
nanoFiles.fileLoc AS nanoFile
FROM nanoProd AS prodTable
LEFT JOIN nanoRelFiles on nanoFiles.rid = nanoRelFiles.file_id
LEFT JOIN nanoProd ON nanoProd.rid = nanoRelFiles.item_id
WHERE nanoRelFiles.scr_type = 'prod' AND nanoRelFiles.fileUse = 'list'
You aren't joining to any table called "nanoFiles." You need to JOIN to that table to be able to SELECT from that column. Something like this:
FROM nanoProd AS prodTable
JOIN nanoFiles on ...
LEFT JOIN nanoRelFiles on nanoFiles.rid = nanoRelFiles.file_id
I want to join two tables having a common column stationcode(TABLE vfdefects & tempsubstation).
The problem is tempsubstation has duplicate column values while i only want the count of each unique values. With my current attempts it is doubling the value for duplicate entries using LEFT JOIN and GROUP BY
TABLE A
ID|stationID|CODE
TABLE B
A|B|C|D|stationcode
The query is as follows:
SELECT DISTINCT TS.substationid,'20130924',ts.employeeid,ts.substationcode,ts.manufacturingproductid,ts.assemblylineid,ts.teamid,ts.subofficecode,
TA.AllowID,ta.OtherAllowanceID,ta.allowname,ta.minbenchmark,ta.maxbenchmark,COUNT(vfd.DefectsID) Achieved
FROM tempsubstation ts
LEFT JOIN TempAllowances ta ON ts.manufacturingproductid=ta.manufacturingproductid AND ts.subofficecode=ta.subofficecode AND ta.teamid=ts.teamid
LEFT JOIN wms_assemblyqcmapping aqc ON ts.subofficeid=aqc.subofficeid AND ts.manufacturingproductid=aqc.manufacturingproductid AND ts.assemblylineid=aqc.assemblylineid
LEFT JOIN wms_vfdefects vfd ON vfd.QCStationCode=aqc.QCStationCode AND ts.SubstationCode =vfd.MFGStationNum AND DATE(vfd.CreationDate)='2013-09-24'
WHERE ta.AllowID=42
GROUP BY ta.minbenchmark,ta.maxbenchmark,ts.substationid
HAVING COUNT(vfd.DefectsID)>=ta.minbenchmark AND COUNT(vfd.DefectsID)<=ta.maxbenchmark
Change:
FROM tempsubstation ts
to:
FROM (SELECT * FROM tempsubstation
GROUP BY columnWithDuplicateValues) ts
How are you running the query? if via PHP then simplify the SQL and run a second SQL in the PHP read loop.
I have this MySQL query
SELECT perms.permission_id, perms.long_title, perms.category, gperms.value FROM
acl_permissions AS perms JOIN acl_group_permissions AS gperms ON
(perms.permission_id = gperms.permission_id AND gperms.group_id = 1) WHERE
perms.type IN ('adm', 'cs') AND perms.simple_title NOT IN ('adm_', 'cs_')
ORDER BY perms.long_title
Basically the bit causing problems is in the ON. I want the query to return all rows from perms based on the where criteria regardless of whether there is a row matching gperms.group_id = 1.
gperms.value would ideally just be an empty string if there was no row matching gperms.group_id = 1.
You want to LEFT OUTER JOIN (or LEFT JOIN for short) gperms instead of just a straight join
SELECT perms.permission_id, perms.long_title, perms.category, gperms.value
FROM acl_permissions AS perms
LEFT JOIN acl_group_permissions AS gperms
ON (perms.permission_id = gperms.permission_id AND gperms.group_id = 1)
WHERE perms.type IN ('adm', 'cs')
AND perms.simple_title NOT IN ('adm_', 'cs_')
ORDER BY perms.long_title
which will return NULL for gperms.value for any row that doesn't join the gperms on the given condition.
If you want an empty string instead of the NULL you can use IFNULL():
SELECT perms.permission_id, perms.long_title, perms.category, IFNULL(gperms.value,'')