How to Join 3 Tables in MySQL? - mysql

I am creating a view for my Database , I am joing 3 tables, Users,personal_info and contact_info, if you notice I have a lot of column names in my Select statement , since i don't want to include primary keys but it seems I have an error here, take a look
CREATE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
The error is
ERROR 1146: Table 'payroll.full' doesn't exist
SQL Statement:
CREATE OR REPLACE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info

quote it with backtics: payroll.new_view
CREATE VIEW `payroll.new_view`

Error on:
LEFT JOIN personal_info on idUser = idPersonal_Info
you need to specify which column on which table equals which one on the other table, like
SELECT a,b,c from table1
LEFT JOIN table2
on table1.a= table2.columnY
in your case:
on USER.idUser = Personal_Info.idPersonalInfo
and the same for the 3rd Join
Another thing is the Comma at the end of the line:
LEFT JOIN personal_info on idUser = idPersonal_Info ,
it doesnt belong there.

Related

SQL Query to retrieve username on reference from a child table

I am working with MySQL. I have two tables, fb_data and fb_not.
Columns of first table are :
id
username
useremail
userpass
fbappid
fbapplink
fbuserid
Columns of second table are :
fk_id
liked_one
liker_one
The liked_one and liker_one columns are referencing fbuserid of fb_data table. Simply they are foreign keys. What I want to do is, to get the names of liker_one column which is in fb_not table. And I want to get the names from username column of fb_data table.
This demonstration shows how the query should work. I want to get the user name of each liker from the fb_data table.
You can get your desired output by inner joining fb_not to fb_data twice; once for liker_one and once for liked_one as below .
SELECT DISTINCT CONCAT (
t2.username
,'('
,t2.fbuserid
,') is the liker who liked '
,t3.username
,'('
,t3.fbuserid
,') post'
) AS Col1
FROM fb_not t1
INNER JOIN fb_data t2 ON t1.liker_one = t2.fbuserid
INNER JOIN fb_data t3 ON t1.liked_one = t3.fbuserid
distinct is used to remove duplicate rows.
Result:
Col1
----------------------------------------------------
nadeem(1234) is the liker who liked nadee(4321) post
You can check demo here
Simply join the table twice:
SELECT d1.username as [Liker], d2.username as [Liked]
FROM fb_not f
INNER JOIN fb_data d1
ON d1.fbuserid = f.liker_one
INNER JOIN fb_data d2
ON d2.fbuserid = f.liked_one
Here is your requested answer. Thanks Let me know if I can help more.
SELECT
username
FROM
fb_data fdata
JOIN
fb_not datan
ON fdata.fbuserid = datan.liker_one

How to handle SQL joins if missing rows on particular table exist

SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
INNER JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13
Above query joins 3 tables (inquiries, users, products) together and gets data from each table.
Sometimes it is possible that items in the 'products' table get deleted. Trying to join products table by a deleted product id will fail the query.
Is there a way that I can assign 'prodData.prodname' a default value and execute query without failing in case of a missing item in products table ?
Why don't use left join insted of inner join ,
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
LEFT JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13

How to select all records from a table by replacing values for two fields from other tables?

This is the table design of an application I need to build. Kindly ignore the Role Table in the image.
Is there any method by which I can select all the records in the table transfer log , but instead of showing "User No" and "Plant No" in the result , I need to show the corresponding Employee No/Username from the user table and corresponding Plant Id from Plant Table.
Try this;)
select distinct
log.LogNo, u.EmployeeNo/Username, p.PlantId, log.StartDate, log.EndDate, log.Active
from TransferLog log
left join UserTable u on u.UserNo = log.UserNo
left join PlantTable p on p.PlantNo = log.PlantNo
Use inner join
select a.*, b.username, c.pantname
from transfer_log as a
inner join User as b a.userno = b.userno
inner join plant as c a.plantno = c.plantno

MySQL Join Query 4 tables

I'm trying to create a query with multiple joins, but it's not working for me.
I have the follow query statement:
SELECT DISTINCT s.per_id
, s.per_key
, s.per_disabled
, p.acc_id
, a.acc_name
, p.approved_acc_id
, p.per_time
FROM acc_permissions p
JOIN svr_permissions s
JOIN acc_general a
JOIN svr_group_permissions g
WHERE a.acc_id = p.acc_id
AND p.per_id = s.per_id
OR a.acc_per_group = g.group_id
AND a.acc_id = p.acc_id
Now if you don't want to examine this query, I understand so I will example my table structure;
First Table (includes users):
acc_general
Second table (includes permissions (linked to users)):
acc_permission
- This table includes rows that are linked to the acc_id in acc_general.
Multiple rows are possible for one unique acc_id in this table.
Third table (includes permissions (liked to groups)):
group_permissions
Now this includes rows that are linked to groups, each group has multiple rows in this table.
Inside acc_general there is a field called; acc_group_id, this is liked with the group_id inside group_permissions
So I need a query that returns all permissions from all players.
But it should not create duplicated permissions for a account.
So if I have an account that has a permission id 1 inside acc_permission and it has permission id 1 inside group_permissions it should ignore it.
It's hard to example, but i hope someone understands what I want.
Regards, Roel
join syntax for your query
SELECT DISTINCT s.per_id AS per_id,
s.per_key AS per_key,
s.per_disabled AS per_disabled,
p.acc_id AS acc_id,
a.acc_name as acc_name,
p.approved_acc_id AS approved_acc_id,
p.per_time AS per_time
from acc_permissions p
join svr_permissions s
ON p.per_id = s.per_id
join acc_general a
ON a.acc_id= p.acc_id
left join svr_group_permissions g
ON a.acc_per_group = g.group_id

How to get the columns from three different tables?

I have three different table as mention below
mRNA_GO
MSU7_LOC
GO_ID
Gene Ontology
GO_ID (Primary Key)
Category
Term
Evidence
miRNA_mRNA
miRNA_ID
MSU7_LOC
Table 1 is connected with table 2 by GO_ID and with table 3 by MSU7_LOC.
I want following columns in my output.
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidence
tabke3.miRNA_ID
I have written two diff query
Query 1
select gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC
from gene_ontology inner join mrna_go on mrna_go.go_id = gene_ontology.go_id
where mrna_go.go_id in ('GO:0009058') ;
Which will give me following columns
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidenc
Query 2
SELECT mrna_go.go_id, mirna_mrna.mirna
from mirna_mrna inner join mrna_go on mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
where mrna_go.go_id in ('GO:0009058') ;
which will give me
table2.GO_ID
tabke3.miRNA_ID
Can any one tell me how can I get the output using only one query not two different query..
Just join third table
SELECT
mg.go_id,
mm.mirna ,
g.go_id ,
g.category,
g.evidence,
g.term ,
mg.MSU7_LOC
FROM mirna_mrna mm
inner join mrna_go mg on mg.MSU7_LOC = mm.MSU7_LOC
inner join gene_ontology g on mg.go_id = g.go_id
where mg.go_id in ('GO:0009058') ;
just add a 2nd join in like;
SELECT gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC, mrna_go.go_id, mirna_mrna.mirna
FROM gene_ontology
INNER JOIN mrna_go ON mrna_go.go_id = gene_ontology.go_id
INNER JOIN mirna_mrna ON mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
WHERE mrna_go.go_id IN ('GO:0009058') ;