I have two tables. These are not them but it is the same principle:
Table:One (artists)
--------------
id (Primary Key)
name
best genre
Table:Two (artist teams)
-------------
id1 (Foreign Key)
id2 (Foreign Key)
I want to select the artist teams where their favorite genres are the same.
My work so far is
SELECT *
FROM Two INNER JOIN One
WHERE ( ).
Im confused as to what to put in the WHERE statement.
I have no idea how to compare the values of the artist's genres to each other!
pseudo code for WHERE:
retrieve id#1's favourite genre
retrieve id#2's favourite genre
compare them
if equal display the related entity from table Two
I've searched for a while looking for a solution and I can't find anything
just like this, I believe it could be a bit a syntax that im missing.
Thanks for any help!
You need multiple joins to the "artists" table:
select t.*, a1.genre
from teams t join
artists a1
on t.id1 = a1.id join
artists a2
on t.id2 = a2.id and a2.genre = a1.genre;
Related
I am trying to setup a SQL database to record the 3 person lineups that have occurred in the game. The structure I have now is:
Player
playerID
playerName
Lineup
lineupID
Lineup_players
lineupID (foreign key)
playerID (foreign key)
I want to find a quick way to check whether a particular set of 3 players are already part of a lineup. Suppose I want to find a lineup with players A,B,C, the solution I can think of is something like:
SELECT t1.lineupID
FROM (SELECT lineupID FROM Lineup_players WHERE playerID=A) t1
INNER JOIN (SELECT lineupID FROM Lineup_players WHERE playerID=B) t2 ON t1.lineupID = t2.lineupID
INNER JOIN (SELECT lineupID FROM Lineup_players WHERE playerID=C) t3 ON t1.lineupID = t3.lineupID
I feel that this is a clumsy solution. Is there a faster query using the same tables, or is there a better way to store the data?
Also, if I have players A,B,C,D,E in my lineup, is there a fast way of finding all lineups with any 3 of these players without checking all 20 combinations?
Maybe you're looking something like this?
select
lineupid,
count(*)
from
Lineup_players
where
playerid in (A,B,C,D,E)
group by
lineupid
having
count(*) >= 3
This will list you the lineups that have 3 or more of the listed ids.
I have two tables from two different databases, and both contain lastName and firstName columns. I need to create JOINa relationship between the two. The lastName columns match about 80% of the time, while the firstName columns match only about 20% of the time. And each table has totally different personID primary keys.
Generally speaking, what would be some "best practices" and/or tips to use when I add a foreign key to one of the tables? Since I have about 4,000 distinct persons, any labor-saving tips would be greatly appreciated.
Sample mismatched data:
db1.table1_____________________ db2.table2_____________________
23 Williams Fritz 98 Williams Frederick
25 Wilson-Smith James 12 Smith James Wilson
26 Winston Trudy 73 Winston Gertrude
Keep in mind: sometimes they match exactly, often they don't, and sometimes two different people will have the same first/last name.
You can join on multiple fields.
select *
from table1
inner join table2
on table1.firstName = table2.firstName
and table1.lastName = table2.lastName
From this you can determine how many 'duplicate' firstname / last name combos there are.
select table1.firstName, table2.lastName, count(*)
from table1
inner join table2
on table1.firstName = table2.firstName
and table1.lastName = table2.lastName
group by table1.firstName, table2.lastName
having count(*) > 1
Conversely, you can also determine the ones which match identically, and only once:
select table1.firstName, table2.lastName
from table1
inner join table2
on table1.firstName = table2.firstName
and table1.lastName = table2.lastName
group by table1.firstName, table2.lastName
having count(*) = 1
And this last query could be the basis for performing the bulk of your foreign key updates.
For those names that match more than once between the tables, they'll likely need some sort of manual intervention, unless there are other fields in the table that can be used to differentiate them?
I would like to know what would be the best way of storing multiple check-boxes values for easy search.
Is there any advantage if for a skills table (football, swimming, dancing ....) I assign unique id or should I use a unique string - the exact word to be stored in the database?
If the answer is "numbers" and the total available options are more than "10" should I start increment the id from ten so I can avoid finding 12, 2, 22 for %2% in case storing is made like 14,23,34 / football,swimming,dancing in the user table.
Is there a better approach?
Use 3 tables:
Person table. This contains single-valued attributed such as name, birthday, etc.
Skills table. This contains the name of each skill.
PersonSkills table. This is a relation table that contains the many-to-many relationship between the first two tables. It has two columns: Person_id and Skill_id, which are foreign keys into the first two tables.
To get all the skills, you join the tables:
SELECT person_name, GROUP_CONCAT(skill_name) skills
FROM Person p
LEFT JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
To get all the people with a particular skill:
SELECT person_name
FROM Person p
JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
WHERE skill_name = "swimming"
I have a table called friends which has id and name and a self join table called friendship which stores the relationship which includes friend_id and friend2_id .
how do i get the names of related friends if a name of a particular frnd is given
example
id name
1 jack
2 kurt
3 jim
and
friendship
f_id f1_id
1 3
So if i give 'jack' i should get jim back
You could do this in one query or two queries, depending on what you want to accomplish.
A simple one could be:
SELECT
f_id,
f1_id
FROM
friendship
WHERE
f_id=1
OR
f1_id=1
And then you can get the specific friends with a statement like:
SELECT name FROM people WHERE id IN(2,3)
Alternative is a self join but the hard part here is that your id might be in both f_id and f1_id so that would need some UNION command or something like (untested):
SELECT
p1.name,
p2.name,
FROM
friendship
INNER JOIN
people AS p1
ON friendship.f_id = people.id
INNER JOIN
people AS p2
ON friendship.f1_id = people.id
WHERE
p1.id=1 OR p2.id=1
I would thoroughly check the speed of these options since they are quite heavy on huge amounts of records. If you measure you need more performance try some alternative. For example when you always put the smallest people.id in f_id and the bigger one in f1_id you might run 2 queries which you union. Alternative is to denormalize a small bit to cache the results if you need them frequently.
It would save you lots of joings for example if you would add the names into the friendship table:
SELECT
f_id,
f1_id,
f_name,
f1_name
FROM
friendship
WHERE
f_id=1
OR
f1_id=1
Hopefully the question explains it well. I have a DB for a Library. Since they can be used many times, and contains more data than just a name, I have a table for Authors. Then there's a table for Books. I have no problem linking Authors to Books via a column called Author_id.
What I'm trying to do is have a column called Author_IDs that contains a list of id's, since a book can have multiple IDs. In the Author_IDs column I have:
<id>3478</id>
<id>6456</id>
Using the ExtractValue function in MySQL I can link the table with one or the other id using:
WHERE Author.id = ExtractValue(Book.Author_IDs,"/id[2]") // to get the second ID.
My question is, I want to be able to automatically display all of the authors of a book, but don't know how to link to it more than once, without looping. How can I get the results to show me all of the authors?
(Or is there a better way to accomplish this?)
Firstly, I have to vote against your storage method. Storing data as xml inside a mysql column should be avoided if possible. If you use a normal approach you will find this problem to be much easier.
Create a table:
book_authors
book_id author_id
------- ---------
1 1
1 2
1 3
2 2
2 4
Then to get all of the authors associated with a certain book it's a simple query.
Select
b.book_id,
b.book_name,
GROUP_CONCAT(a.author_name) AS 'authors'
FROM
book_authors ba LEFT JOIN
books b ON ba.book_id = b.book_id LEFT JOIN
authors a ON ba.author_id = a.author_id
GROUP BY
ba.book_id
Not sure I understand completely. Could something like this do the trick?
select a.* from tblBooks b
left join tblAuthors a on (b.authors = concat('%', a.id, '%')
where b = 'book id';
I would have done it like this
Structure
tblBooks
--------
book_id
book_name
tblAuthors
----------
author_id
author_name
tblBooksToAuthors
-----------------
id
book_id
author_id
Query
select a.*
from tblAuthors a
left join tblBooksToAuthors b2a on (b2a.author_id = a.author_id)
where b2a.book_id = {your book id};