I am trying to a join a table with a second table created from another join. I'm not sure if i'm doing this correctly. I am receving an error code that "Every derived table must have its own alias". Creating an alias is not something I have done before. Could you please let me know how to adjust my query? Thank you.
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
)
ON
questionnaire_status.RR = rep_list.RR
WHERE
questionnaire_Status.Attestation_submitted = '0'
You are currently using subquerys, something you should not do unless absolutely necessary. (<- Performance is very bad)
Better are Multiple Joins are like this:
SELECT
questionnaire_status.*,
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
questionarrie_status
LEFT JOIN
rep_list
ON
questionnaire_status.RR = rep_list.RR
LEFT JOIN
dept_codes
ON
rep_list = dept_codes.dept_id
LEFT JOIN
questionarrie_status
WHERE
questionnaire_Status.Attestation_submitted = '0'
Use the as keyword to alias a subquery thus:
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
rep_list.RR,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
) as tbl_alias
ON
questionnaire_status.RR = tbl_alias.RR
WHERE
questionnaire_Status.Attestation_submitted = '0
but you need to add the join column to the inner select as above
To get the results I think you're trying to get, all you have to do is this:
Select
questionnaire_status.*
FROM
questionnaire_status
LEFT JOIN rep_list ON questionnaire_status.RR = rep_list.RR
LEFT JOIN dept_codes ON rep_list.dept = dept_codes.dept_id
As for the aliases, when you do this you're creating the alias "me":
SELECT * FROM myTable me;
MySQL was complaining about your subquery not having an alias:
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
)
^^^ It wanted an alias here, to cover the subquery
So the next time you have a subquery, do something like this:
(SELECT
rep_list.Last_Name,
rep_list.First_Name,
dept_codes.dept_name
FROM
rep_list
Left Join
dept_codes
ON
rep_list.dept =dept_codes.dept_id
) REP
^^^ MySQL has an alias (named REP) so it's happy now
Related
I am having an issue understanding the inner and left join
I am having the below query in the outsystems
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
The issue is the Select is pulling all the CLD elements without respect to the Project, I am trying to select CLD's whose Project id = Project.Id. I tried both the joins but it keep pulling all the values
Below how the structure looks like
Please try the following: First get the CLDProjects matching with Project then get the CLD from matched records.
FROM {CLD} INNER JOIN (
{CLDProjects} INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
) ON {CLD}.[Id] = {CLDProjects}.[CLDId]
You're probably missing the inner join to CLDProjects on the ProjectParticipant subquery. Add
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId])
on the second EXISTS join conditions, otherwise it will the second results will match the exists for every project the user is in, ignoring the other conditions over CLDProjects. Try the following:
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
Also.. make sure you're not passing #IsJAXPM as 1... otherwise it will definitely return all records.
Let us know if that works. Otherwise, please extend the diagram to show the ProjectParticipant and ProjectParticipantWidget tables as well.
I think you just need to reorder your joins, I tried to reproduce your case (with incorrect joins order), but it gives me error when test the query,
but if you reorder the the joins, it works. note that I am using OutSystems 10
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
order by
Ad_Date_Created
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
I keep getting an error that multi part data can not be bound. Please help to correct query
Try this:
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
order by
Ad_Date_Created
The syntax of your SQL statement is wrong. An ORDER BY clause should come after the JOIN's
I have a query like this
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_page_collections_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'
The error I get is Unknown column 'tbl_page_collections_products.colID in on clause but I don't get that error when the subquery isn't there and that column exists in that table.
Is something conflicting?
tbl_page_collections_products is not in you subquery from clause. Maybe this is what you want:
SELECT
tbl_products.*,
GROUP_CONCAT(tags.name)
FROM
tbl_products,
(SELECT page_collection_name as name
FROM tbl_page_collections
,tbl_page_collections_products
LEFT JOIN tbl_pages ON tbl_page_collections.page_id = tbl_pages.page_id
WHERE tbl_pages.page_name LIKE '%friends%') tags
LEFT JOIN tbl_page_collections
ON tbl_page_collections.page_collection_id = tbl_page_collections_products.colID
LEFT JOIN tbl_pages
ON tbl_page_collections.page_id = tbl_pages.page_id
LEFT JOIN tbl_products
ON tbl_products.product_id = tbl_page_collections_products.product
WHERE
tbl_pages.page_name LIKE '%friends%'
Somehow am not successful with creating the query that I want.
DB is to do with locations, there are the following tables which are relevant
t_location - list of locations incl. field t_location_zipcode, and t_location_id_location
t_zipcodecity - join table just t_zipcodecity_zipcode t_zipcodecity_id_city
t_city - city list with t_city_id_city
t_citystate - join table, t_citystate_id_city, t_citystate_id_state
t_state - list of states with t_state_id_state
Initially I tried to get a list of states with locations using this query:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc
which works fine.
Now what I also need / want which I am failing dismally at is to get the number of locations in each state. I don't know if it can be done in this one query or if i need another, either way I need help!
you can use a count and a group by. Something like this:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
, COUNT(*)
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
GROUP BY `t_state_id_state` , `t_state_name_full`
ORDER BY `t_state_name_full` asc
SELECT t_state_id_state
, t_state_name_full
, COUNT(DISTINCT t_location_id_location) AS locations_number
FROM
t_state
LEFT JOIN
t_citystate ON t_state_id_state = t_citystate_id_state
LEFT JOIN
t_city ON t_citystate_id_state = t_city_id_city
LEFT JOIN
t_zipcodecity ON t_city_id_city = t_zipcodecity_id_city
LEFT JOIN
t_location ON t_zipcodecity_zipcode = t_location_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
Ok thats looking good, my bad with the left joins, i was initially trying to get ALL the states which is why i used them. But i change that to use inner joins
SELECT t_state_id_state,
t_state_name_full,
COUNT(DISTINCT t_location_id_location) AS locations_number
FROM t_state
INNER JOIN t_citystate ON t_citystate_id_state = t_state_id_state
INNER JOIN t_city ON t_city_id_city = t_citystate_id_city
INNER JOIN t_zipcodecity ON t_zipcodecity_id_city = t_city_id_city
INNER JOIN t_location ON t_location_zipcode = t_zipcodecity_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
then i actually end up with a result that looks good !
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.