Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am newbie in programming and I need some help.
I have table (clubs) in DB with teams names and their id.
Next I want to create match in table "matches" in new table named "matches".
How can I tie team's id from "clubs" with teams (home&away)in table matches. I need that id in next actions.
I thought about foreing key, but I cant us it.
Thanks in advance for ur help
Regardless of if the two tables are linked via. a foreign key relationship (although it is good practice to do so) when selecting you can still use a JOIN to connect the two tables. Since you have both a home and away team you need to join the clubs table twice, once for each team.
SELECT m.*, ch.name AS homeClub
FROM matches m
INNER JOIN clubs ch
ON ch.id = matches.home_id
This query will get the match and the home club name, I leave it to you to figure out how to get the away club's name.
Read http://dev.mysql.com/doc/refman/5.0/en/join.html for more information on using joins.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to find the best solution for my needs. I have a budgeting application that each user will have their own budget which will include potentially hundreds or thousands of budget entries. Initially I thought I would have a table for the users and basic info, and then each user would have their own sql table with all of their budget items held within. However, if the user list grows to hundreds or thousands then I would have a large amount of tables since each user would have their own. I also considered a single table to hold everyone's budget entries but I'm not sure that's the correct solution either.
Any advice? Thank you in advance
I think a single table that holds all the budget entries, with a primary key that's referenced by a foreign key in the "users" table, is a good way to go. You can always use something like select * from users u join budgets b on u.userID = b.userID where userID = xxx to get the budget items for a particular user.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently logged in as the first student. I want to retrieve a list of the students I did not send a request to yet. I also want to retrieve a list of students I sent a request to.
Both sender_id and Rec_id are foreign keys referencing to Students.id.
DB schema
Hi Souleiman and welcome to stackoverflow! Your question is not quite clear, you say that sender_id and Rec_id in requestts are both referencing to students, but you can't see to which field exactly. Your requestts table contains different entries in these fields.
Next time you should post the output of SHOW CREATE TABLE students and SHOW CREATE TABLE requestts and a little INSERT statement for the data. Furthermore it would be nice if you would explain the relations and show what you have already tried (see Barmars comment).
I suppose that the relevant foreign key in requestts is Rec_id, so you could try this query:
SELECT s.email FROM students s WHERE s.id NOT IN
(SELECT Rec_id FROM requestts WHERE Rec_id = s.id);
Hope this helps. Have fun learning SQL!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two tables that should somehow be associated. Let's call them table_a and
table_b. A row in table_a can be associated with multiple rows in table_b, and the same goes the other way around. How could I achieve this? Should I use a pivot table?
Both tables have an auto-incrementing id-column.
What you're looking for is called a many-to-many relationship (a given user has zero or more games, a given game has zero or more users). This is typically handled with a "mapping table", e.g. USER_GAMES which has a user_id and a game_id, uniqueness is on the combination of these. http://www.joinfu.com/2005/12/managing-many-to-many-relationships-in-mysql-part-1/ has some good details.
As it is a many to many relationship, an intersection table with the user ID & game ID would be the best. Otherwise you would have to parse the list of game ID's stored in the user table and that would cause performance issues.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm currently trying to build a football database in MySQL. It should store the fixtures from different leagues, plus their results, odds and some other information.
Is the scheme I just created correct or are there any mistakes in it? If so, what can I improve? I'm also not really sure about the link between tblMatch and tblTeams.
Afterwards I want to be able to make a Query where I can select a fixture including the points the home and away team got before the match, plus the average amount of goals of the teams. Like the new fields: 'homeTeamPoints', 'awayTeamPoints' ect.
So my question is: Where should I put these fields? In an extra table or should I put those in the table: 'tblMatch' and store the precalculated values there?
I hope you get what I tried to explain.
Best Regards
-bababow
A few notes:
You will want to replace "homeTeam" and "awayTeam" with "homeTeamID" and "awayTeamId" which will be foreign keys to the tblTeams table. This will enforce that the teams in the match both actually exist.
Remove the matchID and competitionID from the teams. I'm assuming teams can participate in many matches and competitions and therefore this structure will not support that.
What do you want to know about competitions? Is this a tournament? You may want to have a "bracket" and/or "tournament winner" column in there to store the results of the overall tournament.
Those are my main thoughts, other than that it looks OK.
In my perspective if the values of both the fields needs update regularly and table tblMatch data size is large then you should take it into separate table. if both the fields are updates whenever whole record is change then it could be in tblMatch table.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have the following question regarding the SQL server 2008 naming standards.
Should I name a table as singular “Customer” OR “Customers”.
If inside the above table I have an ID column; should I name it “CustomerID”, “Customer_ID”, OR “ID” .
Thanks
I use this notation:
L_TABLE for lookup tables, F_TABLE for fact table.
Plural name if a single row describes, for example, a CUSTOMER. The table contains in fact multiple rows (CUSTOMERS) so it will be meaningful.
Descriptor prefix is useful for join and for readability: if you're not the only one to access to that table it's easier to understand if user query will retrieve CUSTOMER_ID, generic ID, ROW_ID and so on. Think about multiple IDs in the same table referring to different fields (even in other tables).
Personal point: for an ID in a look up table I will always use L_CUSTOMER_ID, it's quite long but totally clear.
Use plural, "Customers", the table will hold many rows.
Use just "ID", its easier to always use the same and easier when writing queries. ("select x where Customers.ID = 5")
On a side note, for other tables referencing the Customers table I would name the referencing column to something that describes the relation. Not just "CustomerID" but "PlacedBy_CustomerID" in the orders table, or "PaidBy_CustomerID" in the invoice table. That way the meaning of the relation becomes a bit more present.