Left Join table error missing aliases - mysql

I wrote a query in mysql to get all records from job_input table and corresponding output records if avaible from job_output table. It gives the following error:
Query: select ji.* from job_input as ji left join (select
SUM(jo.O_Total) AS Total_Output, SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS ...
Error Code: 1248 Every derived table must have its own alias
Below is my query. What am I doing wrong??
SELECT ji.*
FROM `job_input` AS ji LEFT JOIN
(SELECT
SUM(jo.O_Total) AS Total_Output,
SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS S_Output,
SUM(jo.O_M) AS M_Output,
SUM(jo.O_L) AS L_Output,
SUM(jo.O_XL) AS XL_Output,
SUM(jo.O_XXL) AS XS_Output,
SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`)
ON jo.`Job_InputID`= ji.`Job_InputID`

You need to put alias to join table?
SELECT ji.*
FROM `job_input` AS ji LEFT JOIN
(SELECT
jo.`Job_InputID` AS JobID
SUM(jo.O_Total) AS Total_Output,
SUM(jo.O_XS) AS XS_Output,
SUM(jo.O_S) AS S_Output,
SUM(jo.O_M) AS M_Output,
SUM(jo.O_L) AS L_Output,
SUM(jo.O_XL) AS XL_Output,
SUM(jo.O_XXL) AS XS_Output,
SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`) AS table2
ON table2.JobID = ji.`Job_InputID`

Related

no code error in my mysql query yet no results

CREATE TABLE wreport13
AS
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection, users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)
this query should result 4 records but nothing comes out.
please help me to find the error,
when i used this below query
SELECT cdate,COUNT(CID) AS customers
FROM (checkup
INNER JOIN questionaire ON questionaire.QID=checkup.QID)
WHERE checkup.Result=1 and checkup.QID=9
GROUP BY cdate
and result is
cdate customers
2020-05-23 4
2020-05-25 1
2020-05-30 3
2020-05-31 2
the first query is meant to extract the details of the first row of the above query
Sorry, it was a mistake in inner join key with customers table
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection,
users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CuID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)

Left Join issue in mysql query

I have a query
SELECT DISTINCT c.camp_id AS camp_id,
c.camp_key AS camp_key,
c.camp_active AS camp_active,
c.camp_deleted AS camp_deleted,
c.camp_name AS camp_name,
c.camp_cpc AS camp_cpc,
c.camp_destination AS camp_destination,
camp_token1,
camp_token2,
camp_token3,
camp_token4,
camp_token5,
camp_token6,
camp_token7,
camp_token8,
camp_token9,
camp_token10,
token1_field,
token2_field,
token3_field,
token4_field,
token5_field,
token6_field,
token7_field,
token8_field,
token9_field,
token10_field,
group_name,
group_id,
source_id,
source_name,
user_name
FROM mt_campaigns c
LEFT JOIN mt_offers USING (camp_id)
LEFT JOIN mt_groups USING (group_id)
LEFT JOIN mt_traffic_sources USING (source_id)
LEFT JOIN mt_account WHERE c.owner_id = mt_account.user_id
WHERE camp_deleted=0
Now I want to join another table(mt_account) which has a column called user_id and it matches with the the owner_id column of the mt_canpaigns.
How can i edit the join query?
replace
LEFT JOIN mt_account WHERE c.owner_id=mt_account.user_id
for
LEFT JOIN mt_account ON c.owner_id=mt_account.user_id
You need to use ON to define the fields you want to join on instead of WHERE

Not unique table/alias:

Here's a query, i'm getting this error #1066 - Not unique table/alias: 'tbl_cp_list'
I have 2 database ie.
1) grameenphone_bill ---> 1 table ---> tbl_admin
2) android_appstore ---> 2 Tables ---> tbl_cp_list, tbl_list_data
SELECT `grameenphone_bill`.`tbl_admin`.`cp_id`,`grameenphone_bill`.`tbl_admin`.`cp_name`,`android_appstore`.`tbl_cp_list`.`cpid`,`android_appstore`.`tbl_cp_list`.`cpname`,`android_appstore`.`tbl_list_data`.`cp`,`android_appstore`.`tbl_list_data`.`Count`
FROM
`android_appstore`.`tbl_cp_list`
INNER JOIN `grameenphone_bill`.`tbl_admin`
ON `grameenphone_bill`.`tbl_admin`.`cp_id`=`android_appstore`.`tbl_cp_list`.`cpid`
INNER JOIN `android_appstore`.`tbl_cp_list`
ON `android_appstore`.`tbl_cp_list`.`cpname`=`android_appstore`.`tbl_list_data`.`cp`
Please tell , where i'm going wrong?
Thanks!!
You had return wrong table name in last JOIN.
Try this:
SELECT grameenphone_bill.tbl_admin.cp_id,grameenphone_bill.tbl_admin.cp_name,android_appstore.tbl_cp_list.cpid,
android_appstore.tbl_cp_list.cpname,android_appstore.tbl_list_data.cp,android_appstore.tbl_list_data.Count
FROM android_appstore.tbl_cp_list
INNER JOIN grameenphone_bill.tbl_admin ON grameenphone_bill.tbl_admin.cp_id=android_appstore.tbl_cp_list.cpid
INNER JOIN android_appstore.tbl_list_data ON android_appstore.tbl_cp_list.cpname=android_appstore.tbl_list_data.cp
You can also use alias names for tables
SELECT b.cp_id, b.cp_name, a.cpid, a.cpname, c.cp, c.Count
FROM android_appstore.tbl_cp_list a
INNER JOIN grameenphone_bill.tbl_admin b ON b.cp_id = a.cpid
INNER JOIN android_appstore.tbl_list_data c ON a.cpname = c.cp

Get latest row on selecting multiple tables

I have this query:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid,
enc_task.taskid, enc.currentstep, enc.groupid, enc.fdprotocol,
enc_task.linkfile, cus.fname, cus.lname, login.first_name, login.last_name,
login.username, login.user_code, login.id as assigned_id, fp.protocol,
init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN mob_encounters_task enc_task ON enc_task.encounterid=enc.id
JOIN mob_customer cus ON cus.id=enc_task.cus_id
JOIN mob_login login ON login.id=enc.createdby
LEFT JOIN mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN initiation_type init ON init.id=enc.groupid
LEFT JOIN mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id
The query is working, except I need it to return the latest row of enc_task.encounterid when doing this match: enc_task.encounterid = enc.id. Is it possible?
A subquery might do what you're looking for:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid, enc_task.taskid,
enc.currentstep, enc.groupid, enc.fdprotocol, enc_task.linkfile, cus.fname,
cus.lname, login.first_name, login.last_name, login.username, login.user_code,
login.id as assigned_id, fp.protocol, init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN
(select encounterid, orderid, taskid, linkfile from mob_encounters_task order by [FIELD THAT DESIGNATES LATEST VALUE] desc) AS enc_task ON enc_task.encounterid=enc.id
JOIN
mob_customer cus ON cus.id=enc_task.cus_id
JOIN
mob_login login ON login.id=enc.createdby
LEFT JOIN
mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN
initiation_type init ON init.id=enc.groupid
LEFT JOIN
mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id

Would like the rows with max(date)

This works in MYSQL:
SELECT hi_Historie_id,
hi_Klus_id,
hi_Datum_gedaan,
hi_Prijs,
hi_Voldaan,
hi_Datum_voldaan,
hi_Voldaan_via,
max(hi_next_date),
hi_Opmerking
FROM Historie
GROUP BY hi_Klus_id
This gives the right Result: all the rows with the hi_Klus_id with the latest date.
But than I will make a join with an other table:
LEFT OUTER JOIN Glazenwassen ON Historie.hi_Klus_id = Glazenwassen.gw_Klus_id
WHERE Historie.hi_next_date <= CURDATE()
This gives the error #1064.
Can anybody explain me why?
My suggestion would be to create a subquery to return the MAX(hi_next_date) and then join that result to both the Historie and Glazenwassen tables:
select h.hi_Historie_id,
h.hi_Klus_id,
h.hi_Datum_gedaan,
h.hi_Prijs,
h.hi_Voldaan,
h.hi_Datum_voldaan,
h.hi_Voldaan_via,
h.hi_next_date,
h.hi_Opmerking
from Historie h
inner join
(
select max(hi_next_date) hi_next_date, hi_Klus_id
from Historie
group by hi_Klus_id
) h2
on h.hi_Klus_id = h2.hi_Klus_id
and h.hi_next_date = h2.hi_next_date
left join Glazenwassen g
on h.hi_Klus_id = g.gw_Klus_id
where h.hi_next_date <= CURDATE()
When you join the subquery to the Historie table, you will want to join on both the hi_next_date and the hi_Klus_id.