Here I have three tables authors, books_has_authors and books.
Books_has_author is an intermediate table which contains only the primary keys form other two tables. I am trying to write an sql query which will return me all the books name written by a particular author. I am new to relational database concepts as well as writing queries for relational databases. I know some join queries but I'm really confused how to use them for fetching information through an intermediate table. Any help will be appreciated ! :)
You only need to add an additional JOIN, like this:
SELECT b.book_id, b.book_name, a.author_id, a.author_name
FROM books b
JOIN books_has_author ba ON ba.books_book_id = b.book_id
JOIN author a ON ba.author_author_id = a.author_id
WHERE a.author_id = xxx
Try this:
SELECT b.* FROM books b
INNER JOIN books_has_author bha
ON bha.books_book_id = b.book_id
WHERE bha.author_author_id = "$$YOUR AUTHOR ID"
use JOIN like that :-
SELECT *
FROM books
JOIN books_has_author ON books_has_author.books_book_id = books.book_id
JOIN author ON books_has_author.author_author_id = author.author_id
WHERE author.author_id = '123'
Related
I have two SQL tables. The first one structure is simple.
ID Name
----------
The second one is simple too.
CommentID Comment CreatorID
----------------------------------
I want to show in my site all the comments that correspond with the "ID of user"
The problem is we have here a lot of ID and two different tables.
Something like this:
$1= $bdd->query("SELECT * FROM comments WHERE id_user=220281");
$2=$1->fetch();
But its impossible because id user is not on the comments table.
The most simple way to do this is to join the tables like this:
select
users.name,
comms.commentID,
comms.comment
from
userTable users
join commentTable comms
on users.ID=comms.ownersID
where
users.id=1
This will return the users name in each row of data, but you don't need to use it in the output more than once.
It also sounds like you could use a few pointers on SQL queries. Do yourself a favour and have a read of this article I put together a while back.
SELECT c.*
FROM comments c
INNER JOIN users u ON c.id_creator = u.id_user AND
u.id_user = 220281
A simple join will do the trick like this :
SELECT c.comment, u.user_name FROM
Users u
JOIN
Comments c ON
c.creator_id = u.user_id
WHERE
u.user_id=220281
fiddle:http://sqlfiddle.com/#!6/3b28a/1
The default_albums table is used for storing album data.
The default_hottest_categories table is used to store the category
data
The default_album_hc_connect table is used to connect the default_hottest_categories table with the default_albums
table.
I need to be able to display all albums that are apart of the category which is_hottest. the is_hottest column is located in the default_hottest_categories table. The below code is what I have so far:
$q1 = $this->db->query("SELECT * FROM default_albums a, default_hottest_categories d INNER JOIN default_album_hc_connect dc
ON d.id = dc.hottest_categories_id INNER JOIN default_albums ON dc.albums_id = default_albums.album_id
WHERE d.is_hottest = 'Yes'");
I really do not know if this is correct or not. So if you can help me, I'd much appreciate it.
This should work. You had an extra instance of the default_albums table in your FROM clause. I removed that. Also you generally want to join all your tables together. The comma you had in there is used for CROSS JOINS, but is not used that often and is not needed in this case. Also I would recommend only taking the fields you need in your SELECT clause.
SELECT *
FROM default_albums a
INNER JOIN default_album_hc_connect dc
ON a.albums_id = dc.album_id
INNER JOIN default_hottest_categories d
ON dc.hottest_categories_id = d.id
WHERE d.is_hottest = 'Yes'"
I'm having trouble because the main table doesn't have the foreign key i'm trying to search by. Rather, the secondary tables have the key I'm searching by. This means I have to build my information in an array, table by table, joining each with the main table to find the data.
transactions is the main table. deposits is joined to transactions, then I get the deposit info from transactions table. same is then done for payments, invoices, etc. It seems like a less-than-ideal way to design it, or maybe i'm missing something.
i am hoping there is a better way to join the tables in one shot rather than joining them all with union joins.
so currently i say
select * from transactions T left join deposits D on T.id = D.tID where D.account = '123'
union
select * from transactions T left join invoices I on T.id = I.tID where I.account = '123'
union....
etc
any better suggestions? thanks!
i think this should do the work
select * from transactions T
left join deposits D on T.id = D.tID and D.account = '123'
left join invoices I on T.id = I.tID and I.account = '123'
.....
I have the following table structure of my db:
tbl_project
tbl_employee
tbl_deliverable
user_to_deliverable
Where tbl_prjct and tbl_deliverable have a 1-to-many relationship,
tbl_employee and tbl_deliverable have many-many relationships so they are split into user_to_deliverable table.
I want a query to show project_name(from tbl_project), project's deliverables (from tbl_deliverable) and employee name which that specific deliverable is assigned to.
Can I get help writing this query?
Your desired query is much like 89.67% like this :D
SELECT a.ProjectName,
b.deliverables,
d.employeename
FROM tbl_project a
INNER JOIN tbl_deliverable b
ON a.projectID = b.projectID
INNER JOIN user_to_deliverable c
ON b.recordID = c.RecordID
-- or could be the primary key
INNER JOIN tbl_employee d
ON c.userID = d.userID
Approximate solution without knowing your schema. If you want the deliverables/employees concatenated for each project it would probably be best done by retrieving the results and parsing in a non-SQL language (PHP, Python, or whatever you are using). If you want this completely done in SQL you will need to use GROUP_CONCAT().
select tbl_project.name as project,
tbl_deliverable.name as deliverable,
tbl_employee.name as employee
from tbl_project
join tbl_deliverable
on (tbl_project.id = tbl_deliverable.project_id)
join user_to_deliverable
on (tbl_deliverable.id = user_to_deliverable.deliverable_id)
join tbl_employee
on (user_to_deliverable.employee_id = tbl_employee.id)
I have two or more tables which hold values under the same column name. Now when I join these two tables the column names stay the same. When retrieving these values in PHP ($row['name']) I encounter problems since the column 'name' is used twice.
Are there any possible means of separating these two columns inside the query?
SELECT *
FROM stories s
JOIN authors a ON a.id = s.authorid
Table stories
id, name, content, date
Table authors
id, name, date
When i join these two i get one table with similar 'name' columns.
Is there anyway to separate the two tables so the author table has a prefix in front of it? E.g. authors_name /authors_*
Yes, change your SQL this way :
SELECT
s.Id as StoryId,
s.Name as StoryName,
a.Id as AuthorId,
a.Name as AthorName,
FROM stories s
JOIN authors a ON a.id = s.authorid
Then in php, use StoryId, StoryName, AuthorId and AthorName instead of Id or Name.
Hope it helps you.
Yes there is!
SELECT *, stories.name AS s_name, authors.name AS a_name
FROM stories s
JOIN authors a ON a.id = s.authorid
And there you have it. All fields plus two extra! ;)
Hope it helps.
Sorry you can't really do prefixing on a field level, but if you can call 2 queries, just use
SELECT s.*
FROM stories s
JOIN authors a ON a.id = s.authorid
The result set will only contain stories fields and you can use the fields as you normally would with php. Then just do the same again for authors.