DQL relation Many to Many with Symfony - sql

I would like to SELECT the categories in my table product_category through the table categories.
I have a FK many to many between product and category so the table product_category has been created dynamically.
I found that code for get the information in the other table when the FK is not directly in the table I am :
->join(ProductComponentSupplier::class, "pcs", \Doctrine\ORM\Query\Expr\Join::WITH, "p.id = pcs.product")
But in this case, ProductComponentSupplier is a class so it's accessible that way.
I would like to know if there is a way in DQL to get the information or I have to use SQL.
In SQL would be :
SELECT *
FROM category
LEFT JOIN product_category ON category.id = product_category.category_id
I hope I was clear.
Thank you so much for your help.

Related

How to replace the column value with value from other connected table

The code below is my query code of postgresql schema views.
Please assuming this a library table, which is a book list and you have some defined tags can apply on the book itself, and every book will be devided into one category.
CREATE VIEW tagging_books AS
SELECT tags."TagName", books."BookISBN", books."BookName", books."BookCategoryID"
FROM library
INNER JOIN tags on library."TagName_id" = tags."id"
INNER JOIN books on library."BookISBN_id" = books."id"
ORDER BY tags."id"
The schema views inside db will looks like this:
/tags.TagName /books.BookISBN /books.BookName /books.BookCategoryID
Python ISBN 957-208-570-0 Learn Python 1
And the BookCategoryID from table "books" is actually a foreign key of table "category", the table looks like this:
/category
BookCategoryID CategoryName
1 Toolbook
I wonder that, is there anyway to replace the books."BookCategoryID" field to category."CategoryName" by query code? Like the example below.
/tags.TagName /books.BookISBN /books.BookName /category.CategoryName
Python ISBN 957-208-570-0 Learn Python Toolbook
Since they are connected with each other, I think they can definitely being replaced, but I don't know how to do... Thank you.
To include category.name, simply join with table category on the foreign-key constraint like:
select category.name, books.*
from books
join category on books.BookCategoryID = category.BookCategoryID
You can add it to your view creation as well:
CREATE VIEW tagging_books AS
SELECT tags.TagName, books.BookISBN, books.BookName, category.name as "CategoryName"
FROM library
JOIN tags on library.TagName_id = tags.id
JOIN books on library.BookISBN_id = books.id
JOIN category on books.BookCategoryID = category.BookCategoryID
ORDER BY tags.id

How can I take a joined table and use it to reference another table?

I've just started learning SQL and need help with an assignment question. I am asked to look through a dataset about Kickstarter campaigns. I'm asked to find the top 3 categories by amount of backers.
Here is the ER diagram:
ER diagram
In the 'Campaign' Table, there's the 'backers' column, but the 'Category' Table is only related with the Campaign through the 'Sub-Category' Table.
So far, I have been able to Join sub_category.category_id with the sub-category.category_name, but i'm not sure how to take this new Table and join it with Campaign
SELECT C.name AS category_name, SC.category_id, SC.id AS SC_id
FROM Category AS C
JOIN sub_category AS SC ON C.id = SC.category_id
Screenshot
I am hoping to have a table where there is a column for 'Category Name' and 'Backers' and then simply sort it by the number of backers
How should I go about this? Am I on the right track?
SELECT C.name AS category_name, CA.backers
FROM campaign AS CA
JOIN sub_category AS SC
ON CA.sub_category_id =SC.Id
JOIN Category AS C
ON C.id = SC.category_id
order by CA.backers
You can have multiple joins all together in one query.
Secondly there is a connection between Campaign and Sub_Category table which will help to join these two tables.
Later we can then join Category table as these two table has a connection between them based on Category_Id which is a foreign key in sub category table.
At last you can just order by based on Backers.
Let me know if you have any issue or doubt in comments.
And just to take Magnus's answer and rewrite visually, you can better see the hierarchy of the query. See how it closely resembles that of your table relationships
SELECT
C.name category_name,
CA.backers
FROM
campaign CA
JOIN sub_category SC
ON CA.sub_category_id = SC.Id
JOIN Category C
ON SC.category_id = C.id
order by
CA.backers
Notice the indentation to the table its ID is based upon from that prior to it. This way you know which column FROM connecting TO. I have found that if you list the tables in the FROM clause first to show all the HOW tables are related and ON what foreign : primary key relationships, that is the hardest part. Then its just pulling the columns you want after that.

How do I get data from another table?

I have a database with two tables, table 'product' and table 'factory'.
The table product has a column called factory_id and the table factory has both factory_id and 'factory_name'.
I have a bootstrap table which outputs all the fields the product table has, but how do I get the factory_name in there? I know I have to join tables but I don't really know how.
SELECT product.*, factory.factory_name
FROM product
LEFT JOIN factory ON product.factory_id = factory.factory_id;
This should work, but next time try including some code you tried yourself when you ask a question.

how to map a bag/list/set in a class from a table which is got an indirect link to a table

I know the title might sound a bit weird but I've attached a screen of the database design. I have two master tables with a many-to-many relationship in between. But we're using the primary key from the many-to-many table to refer another table called ResourceAllcoation table with the foreign key (ProjectResourceID)
Now what would be the best way to get a bag of ResourceAllocation in the Resource entity? Is there a direct way to do this in nhibernate?
Currently my nhibernate mapping has a one-to-many bag to the ProjectResource table which is then referring the ResourceAllocation table which I feel might not be the best approach.
Please forgive my ignorance. Any thoughts is appreciated.
#kalki, when I implemented your mapping, I figured that the sql query getting generated is
SELECT
*
FROM PROJECTRESOURCE P
LEFT OUTER JOIN RESOURCEALLOCATION R
ON P.PROJECTRESOURCEID=R.ID WHERE P.RESOURCEID=1
but that doesn't work since ProjectResource doesn't have a PROJECTRESOURCEID column
If the query generated was
SELECT *
FROM PROJECTRESOURCE P
LEFT OUTER JOIN RESOURCEALLOCATION R
ON P.ID = R.PROJECTRESOURCEID
WHERE P.RESOURCEID=1
it would work.
You can map this using a Many-to-Many in NHibernate and an IdBag

Simple Query in LINQ

I have 3 tables. Basically, I have this structure:
Customer
--------
IDCustomer
NameCustomer
Product
«««««««
IDProduct
NameProduct
Order
«««««
IDCustomer
IDProduct
How to get a result like this, using LINQ:
Result
««««««
NameCustomer
NameProduct
Thanks in advance!
Assuming you don't have proper relationships set up in the database (or you haven't generated navigation properties for your relationships), you could do the following:
var result = from c in _context.Customer
join o in _context.Order on c.IDCustomer equals o.IDCustomer
join p in _context.Product on o.IDProduct equals p.IDProduct
select new { c.NameCustomer, p.NameProduct }
It would be much easier, though, to add the proper foreign keys to the database and allow Entity Framework (or LINQ to SQL) generate the navigation properties for you. If that were the case, it would be as easy as:
var result = _context.Order.Select(o => new
{
o.Customer.NameCustomer,
o.Product.NameProduct
});
The whole beauty of LINQ is that this should be all automatically done for you with automatically generated objects.
Make sure your table relationships are setup in SQL Server before moving them to your DBML file. After this, your Customer table will have Orders attached to it, in which your Products will be attached to your orders.
Such as selecting from your customer table (LINQ) will give you a list of Customers, in which will have an IENumerable list of Order objects attached to each.
from t1 in Product
join t2 in Order on t1.IDProduct equals t2.IDProduct
join t3 in Customer on t2.IDCustomer equals t3.IDCustomer
select new { t1.NameProduct, t3.NameCustomer}
you can use linqpad.exe to test your linq query directly against your database