In a MySQL database I have two tables linked in a join. One table contains people details and another book details. I want to search the databases for a particular author (principalWriter) and return all the co-authers (additionalWriters) they have worked with.
So I use
SELECT
books.additionalWriters,
people.name
FROM
books
INNER JOIN people ON books.principalWriter = people.personID
WHERE personID = 1;
And this returns each book the author has worked on with the additional writers ID.
However how can I then use these returned IDs to look up their respective names in the name table? Is there a single query I can do to accomplish this?
The problem here is not the query itself but rather the database design. You should have this tables:
Writers(*ID*, name): Will store all writers (principal or not)
Books(*ID*, name): Will store all books
Writers_Books(*WriterID*, *BookID*, Principal): This will store the relationship between the writers and the books and will specify if the writer for that book is principal or not
Primary keys are surrounded by asterisks
Note: You could also remove the Principal field and add it to the Books table, but if a book happens to have to principal writers, you won't be able to solve that with that schema.
Once you update your design, the query will be much easier.
you need to join it again to peoples table. try this:
SELECT a.AdditionalWriters, c.name, b.name
FROM books a INNER JOIN people b ON
a.PrincipalWriter = b.personID
INNER JOIN people c ON a.additionalWriters = c.PersonID
WHERE b.PersonID = 1
Try the following:
SELECT people.name FROM people WHERE personID in
(SELECT
books.additionalWriters,
FROM
books
INNER JOIN people ON books.principalWriter = people.personID
WHERE personID = 1);
Related
I have 2 tables:
master table "writers"
details table "books"
Each writer has a list of books she wrote. I want to get all the writers with the number of books each of them wrote.
Is there a way to do it in one query?
i believe this will work for you. Not sure if there is a more effective way:
select id, (select count(*) from books where books.writer_id = writers.id ) count from writers
I prefer the following non-nested version:
SELECT
writers.id,
writers.name,
count(books.*) as BookCount
FROM
writers
LEFT JOIN books ON writers.id = books.writer_id
GROUP BY
writers.id,
writers.name
I'm creating a database for comics. Right now I have 3 main tables (comics, publishers, people) and 3 junction tables (person2comic, publisher2comic and person2publisher). I want to be able to have a search form that allows searching by any combination of title, issue number, publisher, and person. When referencing only one junction table, I use a variation of this depending on what's being searched for:
SELECT comicTitle, comicIssue, firstName, lastName
FROM person2comic
JOIN comics ON comics.comicID = person2comic.comicID
AND comics.comictitle LIKE "%walk%" ;
If someone were to search by title, publisher and person, I'm not sure how to set up the statement since it would require using two of the junction tables. Would it be a nested query situation or something else?
You can have arbitrarily many joins. Not exactly sure on all of your column names, but this should roughly work:
SELECT *
FROM people
JOIN person2comic p2c ON people.id = ptc.person
JOIN comic ON p2c.comic = comic.id
JOIN publisher2comic pub2c ON comic.id = pub2c.comic
JOIN publisher ON pub2c.publisher = publisher.id
Also note that your schema may be inefficient if you relationships all aren't many-to-many. See my comment.
How can I get all Groups, by the Person ID in this mysql model? I know I need a join colunm or some Hibernate/JPA black magic, but I don't know how to do this.
Here is the model I'm using in study.
Link with image if is not been displayed: http://i.imgur.com/pbCkIVX.png
To reduce space here are the entities:
Github Repository
The following MySQL query will retrieve all groups for a given idPerson
SELECT g.*
FROM `Group` g
JOIN PersonOnGroup pog on g.idGroup = pog.idGroup
WHERE pog.idPerson = myPersonId
I don't know what your hibernate entities look like but something along these lines should work
from Group as group
inner join group.persons as person
where person.idPerson = 1
I have 3 tables called
_partnership,
_partners,
_partnership_arm._partners = stores basic partner information
_partnership_arm = stores partnership arm details
_partnership = stores partners partnership records which includes the partner_id
arm_id which reference _partners.partner_id and _partnership_arm.arm_id.
So as an admin i want to select all details from the _partnership table which join other table reference without a where clause, but am having issue doing it.
here is my code
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners`,`_partnership_arm` ON
_partnership.partner_id = _partners.partner_id
AND
_partnership.arm_id = _partnership_arm.arm_id
I also want a user to be able to select using a where clause
Please how can i achieve this?
Thank you.
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners` ON _partnership.partner_id = _partners.partner_id
JOIN
`_partnership_arm` ON _partnership.arm_id = _partnership_arm.arm_id
I'm having an issue with a mysql query for a search screen at work. I've got the query working using the code I'll post below, but I'm suspicious there is a better way to do it. Mysql are pretty newbie really, I just figure it out as I go along, or try to.
Here is the concept of the database:
There is an Entity, Address, Contact, Client, Group and Facility table involved in my query.
Each Client, Group and Facility is an "Entity" for lack of a better word. Each Entity has it's own Entity ID in the Entity table.
The Entity table houses an address record id and a contact record id.
On the facility search screen, if a user searches a phone number I want to search through the client and group records as well as the facility records. And then return any matching facility information as I normally would.
Here's what I've got so far(I'm doing nothing for address outside of facility records just yet and I've hardcoded some things for the sake of explaining myself):
SELECT facility.FacilityID, facility.Name, contact.PhoneNumber, addy.State addy.ZipCode, facility.ProductionFacilityID,
facility.ProductionEntityID
FROM Facility facility
INNER JOIN ClientGroup cg ON facility.GroupID = cg.GroupID
INNER JOIN Client c ON cg.ClientID = c.ClientID
INNER JOIN Entity e ON facility.FacilityID = e.EntityID
INNER JOIN Entity eg ON cg.GroupID = eg.EntityID
INNER JOIN Entity ec ON c.ClientID = ec.EntityID
INNER JOIN Contact contact ON e.BillingContactID = contact.ContactID
INNER JOIN Contact contactg ON eg.BillingContactID = contactg.ContactID
INNER JOIN Contact cc ON ec.BillingContactID = cc.ContactID
INNER JOIN Address addy ON addy.AddressID = e.PhysicalAddressID
WHERE (facility.FacilityID like '%$searchfor%'
OR contactg.PhoneNumber like '%$searchfor%'
OR cc.PhoneNumber like '%$searchfor%')
AND facility.IsRowActive=1
ORDER BY $searchtype";
Thanks in advance for the help!
Yes, the better way to do this for maintenance purposes is to create a view of only the inner joins and querying the view. Remember in terms of performance there would be little by way of improvement but maintenance of the code would become much easier.
Given your purpose the inner joins are not entirely avoidable unless you decide to change the structure of the tables