I have two tables in my database where the first one has related values of the second one. Just like this:
table "people"
ID | NAME | SCHOOL
-------------------------
1 | john | 2
2 | fred | 1
3 | maria | 3
table "school"
ID | NAME
-------------------------
1 | first school
2 | second school
3 | third school
Ok.
I'm trying to make a select in "people" table and get the "SCHOOL" number replaced by "school" table related id.
So I did this:
"SELECT * FROM people A LEFT JOIN school B ON A.school = B.id"
That's ok!
But If I have to get the "people" ID value in this return, it will be replaced by "school" table ID value.
How can I solve this?
Thanks a lot!
If you are learning SQL, learn to list all the columns you want. Explicitly:
SELECT p.id, p.name, s.name as school_name
FROM
people p LEFT JOIN
school s
ON p.school = s.id;
Notes:
The table aliases are abbreviations for the table names. This makes the query much easier to follow.
A LEFT JOIN is not really needed here, but you are using it.
s.name could be aliased (s.name as school_name) to distinguish it from the person's name.
Related
My database has two tables.
food_table:
+----+----------+---------------+
| ID | NAME | NutrientID |
+----+----------+---------------+
nutrient_table:
+------------+--------------+
| NutrientID | NutrientName |
+------------+--------------+
I want select all rows in food table, but get NutrientName instead of nutrientID.
If I do:
select * from food_table.
I will get NutrientID. Is it possible to get NutrientName in a single query?
SELECT f.*, n.NutrientName FROM food_table f
LEFT JOIN nutrient_table n
ON n.NutrientID = f.NutrientID
You need to make a join inner to tables filtering by field NutrientID , if you look this field as the same in two table and the join works fine.
I need help with MySQL. I am trying to JOIN methods (Left and Inner now)
EDIT: I would also like to INSERT, can someone show me how? Should I use the Trigger? You may show the 2 possible solutions.
I have 4 Tables:
User, Project and User-Project, AssignedProject
Table User-Project has
ID | UserID | ProjectID
Table User has
ID | COMPANYID | UserName |
Table Project has
ID | ProjectName
Table AssignedProject
ID | COMPANYID | ProjectName
What I want to do.
I want to join User-Project table with data of User and Project using AssignedProject as my reference table. I don't know if this makes any sense... I will rephrase.
The association of project and user are in the table AssignedProject. However, I want to have the data in User-Project. Though I just need the ID (which are a foreign key in User-Project)
Example:
AssignedProject
1 | 1001 | AprojectName
Project
1| AprojectName
User
1 | 1001 | Mike
THEN
User-Project
1 | 1 | 1
Is this what you are looking for?
select u.UserName, p.ProjectName
from user_project up
inner join user u on u.id = up.user_id
inner join project p on p.id = up.project_id
For each record in user_project, the query retrieves the name of the associated user and project in tables user and project. This is how I understood your question. I cannot see how table AssignedProject relates to the other tables.
I want to have shown multiple results in 1 fields for a subselect.
As example:
table 1:
tbl1_ID, fistname, familyname
table 2:
tbl2_ID, carbrand
table 3 is the n:n relationship for table 1 and 2
tbl1, tbl2
The Person of table 1 should be able to own several cars (for example Ford and BMW).
The car brand of table 2 is applicable to several People of course.
I want to have listed the cars of each Person in 1 data field
Example:
Mueller | Hans | Ford,BMW
Jaeger | Erwin | BMW,Mercedes,Jaguar
Fritsche | Sascha | Mercedes
How to do this? I cannot do with subselect because it allows only 1 result.
Also, it doesn't work with LEFT JOIN because I want to have shown each Person once only.
Thanks! MR
You could use group_concat and you should use inner join between the two related tables based on table 3 and group by
select a.familyname, a.fistname, group_concat(b.carbrand)
from table_3 c
inner join table1 a on c.table1_id = a.table1_id
inner join table2 b on c.table2_id = b.table2_id
group by a.familyname, a.fistname
I am attempting to JOIN onto two different columns in the first table below from columns in the second and third tables.
I wish to JOIN users.id to job_listings.id to return users.username, and to also JOIN and delimit job_listings.categories to job_categories.id to return job_categories.description via FIND_IN_SET
job_listings
id | employer_id | categories
1 | 1 | 1,2
2 | 1 | 2
users
id | username | type
1 | foo | employer
2 | wat | employer
job_categories
id | description
1 | fun
2 | hak
I desire output that is of the following format:
output
username | type | category | description
foo | employer | 1 | fun
foo | employer | 2 | hak
foo | employer | 2 | hak
I have tried using various permutations of the following code:
SELECT users.username, users.type, job_listings.categories FROM users
JOIN job_listings ON users.id
JOIN job_listings AS category ON FIND_IN_SET(category.categories, job_categories.id)
ORDER BY users.username, category.categories
I know from other answers that I need to use an alias in order to use multiple JOIN operations with the same table, but despite adapting other answers I keep receiving errors related to declaring an alias, or returning output that has a column with the alias but no data returned in that column.
First, you should normalize your design. You should not store integer values in strings. You should not have foreign key references that you cannot declare as such. You should not store lists in strings. Is that enough reasons? You want a junction table for JobCategories with one row per job and one row per category.
Sometimes, we are stuck with other peoples lousy decisions and cannot readily change them. In that case, you want a query like:
SELECT u.username, u.type, jc.id, jc.category
FROM users u JOIN
job_listings jl
ON u.id = jl.employer_id and u.type = 'employer' join
job_categories jc
ON FIND_IN_SET(jc.id, j.categories) > 0
ORDER BY u.username, jc.category;
This query cannot take advantage of indexes for the category joins. That means that it will be slow. The proper data structure -- a junction table -- would fix this performance problem.
I have been trying to figure out how to select data related to one id between to tables without limit it to the joined table. I tried using UNION, Inner join, JOIN, but it limit me to show records that are only in both tables. By example:
Table 1 (users)
id | name | register
1 | John | 2014-03-01
2 | Kate | 2014-03-02
etc..
Table 2 (birthdays by example)
id | user | birthday
1 | 1 | 1989-09-09
Note that kate dont have a record on the birthdays table, if i do:
SELECT U.id, name, register, B.birthday FROM users as U INNER JOIN birthday as B ON B.user = U.id
it will only shows JOHN data, i would like to select all my users and if the record do not exist on the joined table, still be able to select all my users, sort of:
id | name | register | birthday
1 | John | 2014-03-01 | 1989-09-09
2 | kate | 2014-03-02 | null or ''
3
4
etc.
Sorry if its a stupid question but i dont find the light on this one. I would appreciate the help.
Regards
You need a LEFT OUTER JOIN instead of the plain JOIN (also known as INNER JOIN), like this:
SELECT U.id, name, register, B.birthday
FROM users as U
LEFT JOIN birthday as B
ON B.user = U.id
A LEFT JOIN between users and birthday tables will contain all records of the "left" table (users), even if the join-condition does not find any matching record in the "right" table (birthday).
This excellent article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Summary of all JOIN types:
Note: Mysql does not support FULL OUTER JOIN but it can be emulated. Useful articles:
https://stackoverflow.com/a/4796911
http://www.sql-tutorial.ru/en/book_full_join_and_mysql.html
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Use left outer join instead of inner join..
SELECT U.id, name, register, B.birthday
FROM users as U left join birthday as B ON B.user = U.id