I got a problem when I was trying to join 3 tables.
Below are the mysql syntax i was using :
SELECT
FROM carecell
LEFT JOIN staff ON cc.id_pks = staff.id_emp
LEFT JOIN staff ON cc.id_wpks = staff.id_emp
INNER JOIN member ON member.id_member = staff.id_member
Please Help me.. What should I do in order to fix the syntax?
The SQL engine cannot distinguish between the two staff tables in the from clause. You need to give them different names. Something like this:
FROM carecell cc LEFT JOIN
staff s1
ON cc.id_pks = s1.id_emp LEFT JOIN
staff s2
ON cc.id_wpks = s2 .id_emp INNER JOIN
member m
ON m.id_member = s2.id_member
If you join the same table multiple times you need to give that table each time a different alias name so the DB engine can differ between them
SELECT *
FROM carecell as cc
LEFT JOIN staff as s1 ON cc.id_pks = s1.id_emp
LEFT JOIN staff as s2 ON cc.id_wpks = s2.id_emp
INNER JOIN member as m ON m.id_member = s1.id_member
You already used aliases since you gave carecell the alias cc. In the last line of your query I joined s1 but you have to decide which one to take - s1 or s2
SELECT *
FROM carecell
LEFT JOIN staff t1 ON cc.id_pks = t1.id_emp
LEFT JOIN staff t2 ON cc.id_wpks = t2.id_emp
INNER JOIN member ON member.id_member = t1.id_member
Related
I have a table in MySql Db named as newtbl which has 2 columns referenced as a foreign keys they are m_id, p_id. I want that instead of these m_id and p_id their names should be displayed.
Query which I wrote is below, but it has errors.
select newtbl.*, pa.p_name, m.m_id
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
You can try to column an alias name used as
select newtbl.*, pa.p_name, m.m_id as 'mmID'
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
Below is a answer for my question.
select newtbl.u_id, pa.p_name from newtbl left join players pa on newtbl.p_id = pa.p_id
I have a main table:
- cam (id, name, type, desc, tenant_id)
with information normalized into 3 different tables:
- cs with columns (cam_id, st_id)
- ot with columns (cam_id, loc_id, tar_id, tenant_id)
- st with columns (id, code, name)
please note:
- cs.st_id is a foreign key to st.id
- ot.loc_id is a foreign key to st.id,
- ot.tar_id is a foreign key to st.id
My intention is to get the
- st.code value for all ot.loc_id and cs.st_id
(I am not interested in the id's but the their codes which is stored in table st)
This SQL:
- select
cam.id, cam.name, camp.type, cam.desc, st.code as cs.code
from
cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = st.tenant_id;
works in that the last join condition to st table makes the st.codes available.
But what do I have to do to get the ot.loc_id codes?? I can't have multiple from clauses right? or multiple tables in from clause ... right?
Or is there no way out but to make separate SQL statements (which may not be performant i.e making an additional call)?
Thanks!
Take-Away: The join condition does not need to include the table in the from clause! Please see answer below.
No you can't have multiple from clauses
Yes you can have multiple tables in from clause
For example you could do :
select a.id, a.name, a.type, a.desc, b.code as cs.code, b.code
from cam a, st b, cs c, ot d
where a.id = c.cam_id
and a.id = d.cam_id
and st.id = d.loc_id
and b.tenant_id = a.tenant_id
Or your could simply refer to field in joined tables or views in the select statement.
When you do a join, you "join" the table to your select statement and can access them.
Example with join :
select cam.id, cam.name, camp.type, cam.desc, st.code as cs.code, st.code
from cam
left join cs on cam.id = cs.cam_id
left join ot on cam.id = ot.cam_id
left join st on cam.tenant_id = ot.tenant_id
left join st st2 on st.id = ot.loc_id;
Is your intention to join to the st table twice?
select cam.id, cam.name, camp.type, cam.desc, st.code as cs_code , st.code as loc_code
from cam left join
cs
on cam.id = cs.cam_id left join
ot
on cam.id = ot.cam_id left join
st
on cam.tenant_id = st.id left join
st stloc
on ot.loc_id = stloc.id;
According to the columns in your tables, st doesn't have a tenant_id so I changed that to just id.
I am trying to do an inner join on four tables in MySQL. The table names are:
question (id_question, question_text, id_standard)
standard (id_standard, standardtext)
organisation_standard(id_organisation,id_organisation,id_standard)
organisation (id_organisation, organisation_name)
This is my query and it's giving me repetitive values:
select distinct a.question_text, d.organisation_name
from question a
inner join standard b on a.id_standard = b.id_standard
inner join organisation_standard c on b.id_standard= c.id_standard
inner join organisation d on c.id_organisation = d.id_organisation
where a.id_standard = 18;
How can I avoid the repetitive values?
What you need is a left join and not an inner join change the inner joins into left joins and you will get just one row:
select distinct
a.question_text, d.organisation_name
from
question a
left join
standard b ON a.id_standard = b.id_standard
left join
organisation_standard c ON b.id_standard = c.id_standard
left join
organisation d ON c.id_organisation = d.id_organisation
where
a.id_standard = 18
group by a.id_standard;
This diagram from another so answer gives the difference between the different joins
I'm having a real mind blank - This code selects rows quite nicely apart from those entries where I change st.station_id from a value of '1' to a different (but still valid) number but where there are no entries for that station_id in either the station_owner_map table, the organisation table or the cap_gen_data_table. I basically need to amend my sql to ignore any table where there are no entries.
Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name, o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
inner join station_owner_map som1 on st.station_id = som1.station_id
inner join organisation o1 on som1.owner_id = o1.org_id
inner join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1
It's caused by your inner joins. Inner join means there has to be a value in both tables for the record to show up in the result set.
Use left join instead, then only the table 'on the left' has to have a value.
Use left join on tables where the value may not be present.
If you have two tables A and B an inner join will only return the rows from A where the join condition is met. A left join will return all rows from A regardless of if the join condition is satisfied. Columns in the select statement associated with B will be null when a left join is used.
I have only added the left join to the tables you have indicated. If other tables may not satisfy the join condition change the join type from inner to left.
Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name, o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
left join station_owner_map som1 on st.station_id = som1.station_id
left join organisation o1 on som1.owner_id = o1.org_id
left join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1
I'm having some issues with a mysql query.
I'm trying to join together multiple tables.
In one table, I have a list of successful online customers, and on another table I have a table which has the traffic source from which it came. I'm trying to track which customers came from a certain ad. So I want to query those new customers who came on a certain date and from a specific campaign.
SELECT keyword, COUNT(keyword) FROM in_clicks AS ic WHERE ic.create_date LIKE '%2011-08-19%' GROUP BY ic.keyword ORDER BY COUNT(ic.keyword) DESC
INNER JOIN leads AS l ON ls.lead_id = l.id
INNER JOIN lead_status AS ls ON ls.lead_id = ls.id
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
I ran the above code and got the following error.
Error Code: 1064
'INNER JOIN leads AS l ON ls.lead_id = l.id Innner Join lead_status AS ls' at line 2
This should run better :-).
SELECT keyword, COUNT(keyword)
FROM in_clicks AS ic
INNER JOIN leads AS l ON ls.lead_id = l.id
INNER JOIN lead_status AS ls ON ls.lead_id = ls.id
INNER JOIN ads AS a ON ic.ad_id = a.id
INNER JOIN ad_groups AS ag ON a.ad_group_id = ag.id
INNER JOIN campaigns AS c ON ag.campaign_id = c.id;
WHERE ic.create_date LIKE '%2011-08-19%'
GROUP BY ic.keyword
ORDER BY COUNT(ic.keyword) DESC
The order is: SELECT .. FROM ... JOIN ... WHERE ... GROUP BY .. HAVING .. ORDER BY .. LIMIT
All of the INNER JOIN clauses need to be between your FROM and WHERE clauses. It's syntactically invalid to use INNER JOIN following WHERE in the same query.
As pointed out by Bohemian, you also have a syntax error: Innner should be INNER (you have one to many n characters).