Finding records that don't share a relationship [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL - find records from one table which don’t exist in another
I have the following (simplified) schema in MySQL:
An arrow indicates a one (non-arrow side) to many (arrow side) relationship.
I want to determine, for which delivery_zone_weeks, does a customer not have a weekly_order.

It is difficult to understand fully without structures, sample data and expected result, but seems to be risking it a bit you need
SELECT * FROM DELIVERY_ZONE_WEEK WHERE ID_DELIVERY_ZONE_WEEK NOT IN
(SELECT WO.ID_DELIVERY_ZONE_WEEK FROM CUSTOMER C
JOIN SHIPPING_ADDRESS SA
ON C.ID_CUSTOMER = SA.ID_CUSTOMER
JOIN WEEKLY_ORDER WO
ON SA.ID_SHIPPING = WO.ID_SHIPPING
WHERE C.ID_CUSTOMER = #ID_CUSTOMER)

Related

how do i create a calculated field for this table? [duplicate]

This question already has answers here:
Calculate age in MySQL (InnoDB)
(13 answers)
Closed 8 days ago.
SELECT book.BookTitle, author.Name, author.Surname, book.YearofPublication
FROM book
LEFT JOIN author ON book.AuthorID = author.AuthorID;
enter image description here
This is my current table and I need to create a calculated field to tell me how many years the book has been published for but my brain is fried. Can anyone help?
I've tried using xaamp queries but don't see an option.
Try this:
SELECT book.BookTitle, author.Name, author.Surname, book.YearofPublication , YEAR(NOW()) - book.YearofPublication
FROM book
LEFT JOIN author ON book.AuthorID = author.AuthorID;

Get data using join on many to many table [duplicate]

This question already has answers here:
Joining three tables using MySQL
(11 answers)
Closed last year.
I got 4 tables: galleries, areas, stores and gallery_element.
So I need to create a big query in which I could join the data(ex. name column) from galleries, areas, stores which are connected throug gallery_element table.
gallery_element has: id, gallery_id, area_id, store_id columns.
Can someone give me an simple example so I can practice with it further by myself.
Thanks!
I think it can help you to start your query:
select gallery_element.id, galleries.name, areas.name, stores.name from gallery_element
inner join galleries on galleries.id = gallery_element.galleries_id
inner join areas on areas.id = gallery_element.area_id
inner join stores on stores.id = gallery_element.store_id

Mysql Inner Joın At least one match is required [duplicate]

This question already has answers here:
How to select rows with no matching entry in another table?
(11 answers)
How to return rows from left table not found in right table?
(7 answers)
Selecting all items in one table and join with another table, allowing nulls
(4 answers)
Closed 3 years ago.
I created two tables. I keep the topics in one and the comments in this topic in the other.
When I want to view the topics, I want to see the total number of comments on the topic from the comments table.
But if there is not at least one comment in the query I wrote, it does not bring results. How can I see my topics even if there is no comment on my Inner Join query?
Thank you.
Here is my Query:
SELECT portal_topics.id, portal_topics.topicDetail, portal_topics.topicName,portal_topics.topicCategory,portal_topics.topicAuthor, portal_topics.topicTıme, portal_topics.topicAvatar,portal_topics.topicViews, portal_topics.topicLikes, COUNT(portal_comments.id) as totalCommentCount FROM `portal_topics`
INNER JOIN portal_comments ON portal_comments.topic_id = portal_topics.id
WHERE portal_comments.topic_id = portal_topics.id
GROUP BY portal_topics.topicName
Note: The query does not contain any Syntax errors. I have arranged the column names in English for everyone to understand. I may have made a mistake in this section.

find column who has no corresponding rows in another table [duplicate]

This question already has an answer here:
Select rows from one table, join most recent row from other table with one-to-many relationship
(1 answer)
Closed 7 years ago.
Friends i have a problem
I have two tables channel and level
channel table
channel_id, manufacturer_id
level table
level_id,channel_id,sort_order
All channels have certain number of levels in them so like if channel has 4 rows level might have 20.
I want to know which channel has no levels at all. I have tried various solutions from stackoverflow.com but none worked for me as all of them return an empty set but I still have channels with no levels in the database. Please can anybody help. Thanks
You need to do anti join something as
select
c.* from channel c
left join level l on l.channel_id = c.channel_id
where l.channel_id is null

SQL- how to join two tables [duplicate]

This question already has answers here:
Joining two tables in a MySQL
(2 answers)
Closed 8 years ago.
I want to select all from a table called Customer where CustomerID = 'AC001' and I also want to join in another table Pets where the CustomerID(fk) is the same as the CustomerID in Customer table is that possible?
Customer table
CustomerID,TypeOfCustomer, FName,Adress,City,State,Zipcode,PhoneNr,FaxNr
Pets table
PetID,FName, Animal,Breed,Gender,DoB,CustomerID(fk)
This is a very basic SQL question, here you go: to join the two tables together, do this:
SELECT *
FROM Customer
JOIN Pets ON Pets.CustomerId=Customer.CustomerID
To filter for CustomerID = 'AC001', add a WHERE clause.
Note also that the result won't contain customers without any pets. If you want to include those as well, do a LEFT JOIN instead.
Generally, I'd recommend reading a good beginner-level book on SQL and relational databases to make sure you grasp the underlying concepts here.
Select //fields you want// from Customer C, Pets P
WHERE C.CustomerID=P.CustomerID