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

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;

Related

How to get the items not found in a Mysql table [duplicate]

This question already has answers here:
SQL IN Clause - get back IN elements that did not match
(5 answers)
Closed 7 months ago.
I have a random input of identifiers (primary key), and I need to return the identifiers which were not found in the mySql table.
Lets say my identifier list is ['FEB221571','1221SC170','75960620447200111', 'ABC12344'].
My table has the first three identifiers so I can find them on the table but I wish the last one to be returned as it cannot be found in the table. How do I implement that?
select * from identifier_list a
where not exists (select 'x' from some_table b and a.id = b.id);

Multi Join Table in Mysql returning only one row [duplicate]

This question already has an answer here:
SQL counting all rows instead of counting individual rows
(1 answer)
Closed 1 year ago.
Hi I am seeking help with my code, currently it is returning only one row and I am utterly stuck with this one and cant seem to fix it. I'm hoping another persons insight could help me.
use library;
#3
Select title, count(loanId) as 'Number of loans'
From book
Join bookCopy ON bookCopy.isbn = book.isbn
Join loan ON bookCopy.copyId = loan.copyId
Where dateBack IS NULL
Order By title;
That is because COUNT.
use GROUP BY title
Select title, count(loanId) as 'Number of loans'
From book
Join bookCopy ON bookCopy.isbn = book.isbn
Join loan ON bookCopy.copyId = loan.copyId
Where dateBack IS NULL
GROUP BY title
Order By title;

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.

PDO - select data from two tables [duplicate]

This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 6 years ago.
I want to take all the data from two tables with the same user_id, but I don't know what type of join to use and I want to be sure with the syntax of the query.
"users" table:
"schedule" table:
Here's a simple LEFT JOIN example:
SELECT * FROM `users` LEFT JOIN `schedule` ON `users`.`userid` = `schedule`.`user_id`
WHERE `users`.`userid` = ?
this will collect everything from both tables and then you can output it accordingly.

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

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)