Ok, I have two tables in my database. I have a table books, that has (id, title, author), and I have another table orders which has (id, bookid, authorid, fromdate, todate).
I am just to get all the books, that are available to order/reserve. Meaning, all the books from the books table, where in the orders table there should be no orders.bookid the same as any books.id
So, I've made this query:
SELECT books.title, books.author
FROM books
INNER JOIN orders
ON orders.bookid ORDER <> books.id
looks like I've found it, looks to work with:
SELECT * FROM books LEFT JOIN orders ON books.id = orders.bookid WHERE orders.bookid IS NULL
What do you think ?
So, I have 6 books in my example, with ids 1-6, and I have in orders table two orders, where the booksid(s) are 3 and 4. Now, if I run my query it shows me the books from 1-2 and 5-6 (twice), and also the books 3 and 4 once.
I don't know exactly what I'm doing wrong, but anyway, I'm kind of stuck.
You can try this:
SELECT b.title, b.author
FROM books b
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.bookid = b.id);
Why not give your tables alias names and join on the id column?
SELECT
B.TITLE,
B.AUTHOR
FROM BOOKS B
INNER JOIN ORDERS O ON B.ID = O.BOOKID
Related
I have two MySQL-tables:
Persons (pid,name,companyID,companyName)
Orders (oid,companyID,details)
Now I want to count the number of order_id for each companyName as following:
Name Total
-------------------
CompanyName1 : 1200
CompanyName2 : 758
CompanyName3 : 11
I used this query but it's not working properly.
SELECT count(o.oid) as total,p.companyName
FROM orders as o, persons as p
WHERE o.companyID = p.companyID
GROUP BY p.companyName
Use join and group the result by p.companyID
SELECT p.companyName, count(o.oid) as total
FROM orders as o join persons as p
on o.companyID = p.companyID
GROUP BY p.companyID
If you are missing the companies without any orders you can use a left join.
SELECT p.companyName, count(*) as total
FROM persons p
LEFT JOIN orders o ON o.companyID = p.companyID
GROUP BY p.companyID, p.companyName
Please do not use the old, legacy join syntax any more - it is outdated since 1992.
Your data model looks messed up. That you have company ids and names in the person table but no corresponding companies table is highly suspicious.
In any case, presumably there can be multiple rows per company. You can condense the persons table and then join:
SELECT c.companyName, COUNT(*) as total
FROM orders o JOIN
(SELECT DISTINCT companyId, companyName
FROM persons p
) c
ON o.companyID = c.companyID
GROUP BY c.companyName;
However, you should fix the data model so you have a real bona fide companies table -- especially because you seem to care about that entity.
i am currently working with a MYSQL-Database whichhas three tables:
Books, Keywords and KeywordAssignment.
The tables Books and Keywords are in a many to many relationship therefore the table KeywordAssignment.
Now to my question: I want to search for a book with multiple (max: up to ten) keywords.
I've already tried a self join:
SELECT BookID
FROM Keywords K1 INNER JOIN
Keywords K2
ON K1.KeywordAssignmentID=K2.KeywordAssignmentID INNER JOIN
KeywordAssignment
ON KeywordAssignment.KeywordAssignmentID=K1.KeywordAssignmentID INNER JOIN
Books
ON KeywordAssignment.BookID=Books.BookID
WHERE K1.Keyword='Magic' AND K2.Keyword='Fantasy'
The problem is it only works if the given Keyword are in the right order. If they aren't there are more than one.
I appreciate your help thank you very much!
You need to GROUP BY BookID and a HAVING clause with the condition that both keywords are linked to that BookID:
SELECT b.BookID, b.Title
FROM Books b
INNER JOIN KeywordAssignment ka ON ka.BookID = b.BookID
INNER JOIN Keyword k ON k.KeywordID = ka.KeywordID
WHERE k.Keyword IN ('Magic', 'Fantasy')
GROUP BY b.BookID, b.Title
HAVING COUNT(DISTINCT k.Keyword) = 2
This code will return books that are linked to both 'Magic' and 'Fantasy'.
If you want either of the 2 keywords then remove the HAVING clause.
If I understand your question correctly, you want to query for books that have multiple key words. The key word there is have. I don't have MYSQL but the query should look something like this:
SELECT B.BookID, COUNT(*) as NumberOfKeywords FROM Books B
INNER JOIN KeywordAssignment KA
ON B.BookID = KA.BookID
INNER JOIN Keywords K
ON KA.KeywordID = K.KeywordID
GROUP BY B.BookID
HAVING NumberOfKeywords > 0 AND NumberOfKeywords <= 10
What we are doing is grouping by each book and then selecting the ones that have more than 0 keywords and less than 10.
Ok I have two mysql table one called books and the other reading_tracking.
I have written the join query below:$query = "SELECT * FROM books inner join reading_tracking on book_num = book_id;
My dilemma is that i don't want all the from books i want a query which in theory would look something like this:
"SELECT * FROM books
where reading_status = 1 inner
join SELECT * FROM reading_tracking
on book_num = book_id";
If i try this as suggested:
SELECT * FROM books b
INNER JOIN reading_tracking rt
ON b.book_id = rt.book_num
WHERE rt.reading_status = 1;
The thing is in my program not all books in books table are in reading_tracking, user have to make an additional step, so i don't want books that are similar in both tables i want all the books from books where reading_status = 1; but want all the books from reading_tracking.
Your syntax should have the SELECT [x] FROM followed by the INNER JOIN, and then you need an ON to denote which field is synonymous between the two tables. In your example, I'm assuming you have a column book_id in books, and a column book_num in reading_tracking.
The WHERE clause should come after the ON, and additionally should specify which of the two tables you want to look for the column in.
This can be seen in the following:
SELECT * FROM books b
INNER JOIN reading_tracking rt
ON b.book_id = rt.book_num
WHERE rt.reading_status = 1;
This will search books for any row that has a reading_status of 1 in the reading_tracking table.
I assume column book_num is from books table, column book_id is from reading_tracking table. First join the 2 tables then use WHERE to filter the condition:
SELECT * FROM books b
INNER JOIN reading_tracking r
ON b.book_num = r.book_id
WHERE b.reading_status = 1;
You can use a subquery before doing the JOIN as shown below:
SELECT *
FROM (SELECT * FROM books WHERE reading_status=1) A
INNER JOIN reading_tracking B on B.book_num=A.book_id;
See Using MySQL Alias To Make The Queries More Readable
I need to display the first and last names of people who have sold items at a price of over $20, I need to join the title and author table but they do not have any common keys? How do I go about doing this? I only need to display their names as input, here's what I have so far and I am getting blank results. I'm just learning, so try not to get too technical
Here are the column names:
authors: pk- au_id
au_fname
au_lname
titles: pk- title_id
title
price
fk- pub_id
This is what I have so far:
select price, au_lname, au_fname
from titles JOIN authors
on authors.au_id=titles.pub_id
It looks like you also have a titleauthor table. That would help with the query in your sample code. However, this table is not needed in this case, as your question is really asking about the sales person, rather than the author. You really want something like this:
select distinct e.firstname, e.lastname
from sales s
inner join employee e on e.empl_id = s.empl_id
where s.itemprice >= 20
Of course, I had to make wild guesses about your column names here. You can extend this to include information about the title and author(s) of those items like so:
select distinct e.firstname as SalePersonFirstName, e.lastname as SalesPersonLastName
, t.Title, a.Lastname As AuthorLastName, a.firstname as FirstName
, s.itemprice
from sales s
inner join employee e on e.empl_id = s.empl_id
inner join titles t on t.title_id = sales.title_id
inner join titleauthors ta on ta.title_id = t.title_id
inner join authors a on a.au_id = ta.au_id
where s.itemprice >= 20
I have a pretty simple MySQL question. I have two tables, Customer and Orders. Customer table has fields (id, name) and Order has fields (id, customerID, and item).
I can find which customer bought product A and customers that bought product B with the following query in MySQL.
SELECT DISTINCT c.`id`, c.name, o.`item`, o.qty FROM `customer` as c
INNER JOIN order AS o ON (c.`Id` = o.`customerID`)
where o.`item` ="Product A"
Union
SELECT DISTINCT c.`id`, c.name, o.`item`, o.qty FROM `customer` as c
INNER JOIN order AS o ON (c.`Id` = o.`customerID`)
where o.`item` ="Product B"
How can find the difference and similarity in these two result sets?
1) I.e. Customers that bought only product A but did not by product B
2) I.e. Customers that bought both product A and B
Thank you for your assistance.
D
You can try using the LEFT OUTER JOIN to get the result.