Join tables with one table having multiple identical IDs - mysql

I want to join three tables. One of these tables (modx_article_category) can have rows with identical IDs (articles have multiple categories).
I would like to put the values of these joins in a single column where the results are comma separated.
Here's my code so far:
I was looking for a solution but am not even sure what to google...
CREATE TABLE article_en AS
SELECT *
FROM mod_article_c, category_c, modx_article_category
WHERE mod_article_c.article_id = modx_article_category.article
AND modx_article_category.category = category_c.category_id
AND mod_article_c.article_lang = "en"
AND category_c.category_lang = "en"
DB samples:
https://raslan.de/index.php/s/cK9mxGyj9wKzFsS
This only selects one category even though there might be more.
If you need further infos just let me know.
Thanks in advance.

You can use group_concat.
SELECT c1.category_id,
Group_concat(DISTINCT article ORDER BY article)
FROM mod_article_c c
INNER JOIN modx_article_category c1
ON c.article_id = c1.article
INNER JOIN category_c c2
ON c1.category = c2.category_id
WHERE c.article_lang = "en"
AND c2.category_lang = 'en'
GROUP BY category_id;
above query will return all the articles for each category_id/category

Related

How to count rows in table with condition WHERE?

There is a query that to count rows from table question_category:
SELECT
idSpecialization,
thematicspecialization.SpecializationName AS SpecializationName,
SubscrubeToUsersIdNote,
COUNT(question_categoryId) AS CNT,
COUNT(idSubscrubeToUsers) AS SUBS
FROM thematicspecialization
LEFT JOIN question_category ON question_category.question_categoryIdCategory = thematicspecialization.idSpecialization
LEFT JOIN question a ON a.idQuestion = question_category.question_categoryIdQuestion
LEFT JOIN subscrubetousers ON (subscrubetousers.SubscrubeToUsersIdNote = idSpecialization AND subscrubetousers.SubscrubeToUsersType = 4 AND SubscrubeToUsersStatus = 1)
WHERE a.country = 1
GROUP BY idSpecialization
Need display the category names of thematicspecialization with the condition WHERE a.country = 1 for joining tables question a ON a.idQuestion = question_category.question_categoryIdQuestion
In my case - the categories are not displayed.
This is too long for a ocmment.
This is your query, fixed up a bit to use table aliases so it is easier to read:
SELECT ts.idSpecialization, ts.SpecializationName, sts.SubscrubeToUsersIdNote,
COUNT(question_categoryId) AS CNT,
COUNT(idSubscrubeToUsers) AS SUBS
FROM thematicspecialization ts LEFT JOIn
question_category qc
ON qc.question_categoryIdCategory = ts.idSpecialization LEFT JOIN
question q
ON q.idQuestion = qc.question_categoryIdQuestion LEFT JOIN
subscrubetousers sts
ON (sts.SubscrubeToUsersIdNote = ts.idSpecialization AND
sts.SubscrubeToUsersType = 4 AND
sts.SubscrubeToUsersStatus = 1)
WHERE q.country = 1
GROUP BY idSpecialization;
Here are things that I notice:
The join between ts and qc is on IdCategory and idSpecialization. That seems like a strange combination of names.
The join between sts and ts is partly on SubscrubeToUsersIdNote and idSpecialization. That seems like a strange combination of names.
The where condition is turning some left joins into inner joins -- calling into question why you want left join anyway.
The two count columns are going to return very similar numbers, because they are counting non-NULL values on join keys`.
Your question is about "categories" and their "names". However, based on your query, it is entirely unclear what fields these refer to.
Based on this query, I have no idea what you are really trying to do. You should probably write another question, with sample data and desired output to express what you are trying to do. This question already has two answers. Clarifying it so it would be understood would (probably) invalidate the answers, which is impolite.
Not clear.
But I guess. You have a join with condition like
subscrubetousers.SubscrubeToUsersIdNote = idSpecialization AND subscrubetousers.SubscrubeToUsersType = 4 AND SubscrubeToUsersStatus = 1be this
May be this condition conflict thematicspecialization field

Need mysql query to pull data from two tables

So after helpful feedback from my original question, I now have this query:
SELECT sessions.id, sessions.title, sessions.abstract, sessions.presenters, sessions.proposal_id, proposals.outcomes, proposals.CategorySelection, proposals.research3, proposals.research4, proposals.research5, proposals.research6, proposals.innovation3, proposals.innovation4, proposals.innovation5,proposals.innovation6, proposals.application3, proposals.application4, proposals.application5, proposals.application6, proposals.integration3, proposals.integration4, proposals.integration5, proposals.integration6, proposals.references, proposals.organization
FROM sessions, proposals
INNER JOIN proposals ON proposals.id = sessions.proposal_id
WHERE sessions.id = '$id
LIMIT 1;)
that is getting me nowhere fast. What am I doing wrong?
Original question:
I need to pull several fields from one table and several more from a second table. The criteria is that a field called proposal_id match the id field of the second table. I am fairly new so this is what I have so far. It is not working, but not sure how to make it work.
(SELECT `title`,`abstract`,`presenters`,`proposal_id` FROM `sessions` WHERE `id`='$id')
UNION
(SELECT `outcomes`,`CategorySelection`,`research3`,`research4`,`research5`,`research6`,`innovation3`,`innovation4`,`innovation5`,
`innovation6`,`application3`,`application4`,`application5`,`application6`,`integration3`,`integration4`,`integration5`,`integration6`,`references`,`organization` FROM `proposals` WHERE `id`= `sessions`.`proposal_id`)
LIMIT 1;
You need to use JOIN not UNION
select
s.*,p.*
from `sessions` s
inner join `proposals` p on p.id = s.proposal_id
where s.id = '$id'
This is how you can join both the tables using the common key between.
You can select the specific fields instead of .* by specifying the column names as
s.col1,s.col2,p.col1,p.col2
etc
Try to use JOINS, where you can match the related fields from both the tables , this is the most convenient way to fetch records from multiple tables
UNION is used when you want to combine two queries
select a.id,b.some_field from table1 as a
INNER JOIN table2 as b ON b.prospal_id = a.id

Retrieve matching data twice from same LEFT JOIN table?

Information
Is there a way that I can retrieve two bits of information from another(same) table twice in one LEFT JOIN?
Attempt
Below is the SQL query I tried to put together in hopes would work. I hope you can see what I am trying to do from it.
SELECT cards.*, list.name FROM cards
LEFT JOIN list ON cards.main = list.id AS main_name
AND cards.enemy = list.id AS enemy_name
WHERE cards.id = 1
As you can see above I am trying to retrieve the names of two values cards.main AND cards.enemy from the list table.
Thank you, and any questions will be answered asap!
You have to join the list-table twice:
SELECT cards.*, main_name.name, enemy_name.name
FROM cards
LEFT JOIN list AS main_name ON cards.main = main_name.id
LEFT JOIN list AS enemy_name ON cards.enemy = enemy_name.id
WHERE cards.id = 1

Get "non-existing" values from database

I am having trouble with my database. I have three corresponding tables, where I in the first is holding some pages and in the second one holding some fields.
The third one is a table holding content to all fields in the pages.
Tables:
pages(PageID, Name)
fields(FieldID, FieldName);
pagefields(PageFieldID, FieldID, PageID)
pagefieldcontents(PageFieldContentID, PageFieldID, FieldID, PageID, Content)
What I need is to make sure, that EVEN if there in the table "pagefields" is NO value to a field, but if the field is related to the page (in the pagefields table), it will get returned as a row with value as an empty string.
What I am doing now is the following:
SELECT
pfc.ContentID as PFC_ContentID,
pfc.Content as PFC_FieldContent,
pfc.FieldID as PFC_FieldID
FROM
pagesfieldcontents pfc
INNER JOIN
pagefields pf
ON pf.PageID = pfc.PageID
INNER JOIN
fields ptf
ON pf.FieldID = ptf.FieldID
INNER JOIN
pages p
ON p.PageID = pf.PageID
WHERE
(some where-statement)
ORDER BY
somefield desc
Hope you can understand my question - have a nice day.
INNER JOINs require there to be a row in the joined table for the first tabke's row to be returned. But you may have fields that font have content and you shill want those returned...
Change all your INNER joins to LEFT joins:
SELECT
pfc.ContentID as PFC_ContentID,
pfc.Content as PFC_FieldContent,
pfc.FieldID as PFC_FieldID
FROM pages p
LEFT JOIN pagefields pf
ON p.PageID = pf.PageID
LEFT JOIN fields ptf
ON pf.FieldID = ptf.FieldID
LEFT JOIN pagesfieldcontents pfc
ON p.PageID = pfc.PageID
AND pf.FieldID = pfc.FieldID
WHERE some where-statement
ORDER BY somefield desc
I've also rearranged the table order and join conditions to what I guess you need.

Getting Repeated values in SQL

I was desperately trying harder and harder to get this thing done but didn`t yet succeed. I am getting repeated values when i run this query.
select
tbl_ShipmentStatus.ShipmentID
,Tbl_Contract.ContractID,
Tbl_Contract.KeyWinCountNumber,
Tbl_Item.ItemName,
Tbl_CountryFrom.CountryFromName,
Tbl_CountryTo.CountryToName,
Tbl_Brand.BrandName,
Tbl_Count.CountName,
Tbl_Seller.SellerName,
Tbl_Buyer.BuyerName,
Tbl_Contract.ContractNumber,
Tbl_Contract.ContractDate,
tbl_CountDetail.TotalQty,
tbl_CostUnit.CostUnitName,
tbl_Comission.Payment,
tbl_Port.PortName,
Tbl_Contract.Vans,
tbl_Comission.ComissionPay,
tbl_Comission.ComissionRcv,
tbl_CountDetail.UnitPrice,
tbl_Comission.ComissionRemarks,
tbl_CountDetail.Amount,
tbl_LCStatus.LCNumber,
tbl_ShipmentStatus.InvoiceNumber,
tbl_ShipmentStatus.InvoiceDate,
tbl_ShipmentStatus.BLNumber,
tbl_ShipmentStatus.BLDate,
tbl_ShipmentStatus.VesselName,
tbl_ShipmentStatus.DueDate
from tbl_ShipmentStatus
inner join tbl_LCStatus
on
tbl_LCStatus.LCID = tbl_ShipmentStatus.LCStatusID
inner join Tbl_Contract
on
tbl_LCStatus.ContractID = Tbl_Contract.ContractID
inner join Tbl_CountDetail
on Tbl_Contract.ContractID = Tbl_CountDetail.ContractId
inner join tbl_Comission
on
tbl_Comission.ContractID = Tbl_Contract.ContractID
inner join Tbl_Item
on
Tbl_Item.ItemID = Tbl_Contract.ItemID
inner join Tbl_Brand
on Tbl_Brand.BrandID = Tbl_Contract.BrandID
inner join Tbl_Buyer
on Tbl_Buyer.BuyerID = Tbl_Contract.BuyerID
inner join Tbl_Seller
on Tbl_Seller.SellerID = Tbl_Contract.SellerID
inner join Tbl_CountryFrom
on Tbl_CountryFrom.CountryFromID = Tbl_Contract.CountryFromID
inner join Tbl_CountryTo
on
Tbl_CountryTo.CountryToID = Tbl_Contract.CountryToID
inner join Tbl_Count
on
Tbl_Count.CountID = Tbl_CountDetail.CountId
inner join tbl_CostUnit
on tbl_Comission.CostUnitID = tbl_CostUnit.CostUnitID
inner join tbl_Port
on tbl_Port.PortID = tbl_Comission.PortID
where tbl_LCStatus.isDeleted = 0
and tbl_ShipmentStatus.isDeleted =0
and tbl_LCStatus.isDeleted = 0
and Tbl_CountDetail.isDeleted = 0
and Tbl_Contract.isDeleted = 0
and tbl_ShipmentStatus.LCStatusID = 5
I have also attached a picture of my result set of rows.
Any suggestions why this is happening would really be appreciable.
Result Set
Typically this happens when you have an implicit partial cross join (Cartesian product) between two of your tables. That's what it looks like to me here.
This happens most often when you have a many-to-many relationship. For example, if a single Album allows both multiple Artists and multiple Songs and the only relationship between Artists and Songs is Album, then there's essentially a many-to-many relationship between Artists and Songs. If you select from all three tables at once you're going to implicitly cross join Artists and Songs, and this may not be what you want.
Looking at your query, I see many-to-many between Tbl_CountDetail and tbl_Comission through Tbl_Contract. Try eliminating one of those joins to test to see if the behavior disappears.
Try using the DISTINCT keyword. It should solve your issue
Select DISTINCT ....
Wait as far as I can see your records are not duplicates.
HOWEVER
Notice the CountName column and Shipment ID column
The combination is unique for every row. Hence the values are unique as far as I can see. Try not selecting CountName.
Well if you have distinct rows its not a duplication problem. The issue is during the join a combination is occurring you don't want it to duplicating the results.
Either don't select CountName or you have a mistake in your data.
Only one of those rows should be true either 6 with Count2 or 6 with Count1. Likewise for 7. The fact that your getting both when your not supposed to indicates a logic mistake