Unknown column in 'IN/ALL/ANY subquery' - mysql

Why am I getting this error? Error is showing up in the OR clause, but I'm selecting gl_ID inside the SELECT statement.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN ( //problem is here
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)
Error message:
Unknown column 'gl_ID' in 'IN/ALL/ANY subquery'
Should I be using an AS or a HAVING?

gl_ID is not accessible in the OR clause because it doesn't exist in t_table. An inner join under the first FROM clause solves the problem.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
INNER JOIN gl_globals ON (t_ID = gl_ID) // fix
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN (
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)

Related

UPDATE - SELECT - MYSQL #1093 - You can't specify target table 'temp1' for update in FROM clause

I can't find solution to correct this big query, I always receive an error from database.
I have tre tables and I need to make changes on some attribute on some condition:
UPDATE o36t_orders as temp1,
mytable as temp2
SET
temp1.bonifico = 1,
temp2.ultimo = 1
WHERE
temp1.id_order IN (
SELECT
id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
AND temp2.id IN (
SELECT
id
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
Since the subqueries of the 2 IN clauses are identical (except the retuned column), I think that you can do what you want by a straight inner join of the 2 tables and that subquery (returning both columns):
UPDATE o36t_orders temp1
INNER JOIN (
SELECT
id, id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
) t ON t.id_order = temp1.id_order
INNER JOIN mytable temp2 ON temp2.id = t.id
SET
temp1.bonifico = 1,
temp2.ultimo = 1

Sql Not unique table/alias

I have sql query:
SELECT leads.lead_id,
attributes.code AS attributeCode,
leads_notes.content AS noteContent,
leads_notes.task_type_id,
task_types.type_name
FROM leads,
leads_notes
INNER JOIN task_types
ON task_types.task_type_id = leads_notes.task_type_id
INNER JOIN leads_attributes
ON leads_attributes.lead_id = leads.lead_id
INNER JOIN attributes
ON attributes.attribute_id = leads_attributes.attribute_id
INNER JOIN leads_notes
ON leads_notes.lead_id = leads.lead_id
WHERE ( leads.ambassador = 1
OR leads.rents_bike = 1 )
AND ( leads.city <> '' )
AND ( leads.address <> ''
OR leads.address2 <> '' )
AND ( leads.country_id <> '' )
AND ( leads_attributes.attribute_id IN ( $attributes_id ) )
AND ( leads.lead_id = $lead_id )
But I get error:
Syntax error or access violation: 1066 Not unique table/alias:
'leads_notes''
How can I solve it? Thanks.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
In your case you also had lead_notes twice in the FROM clause. It is easier to write and read the query if you use table aliases:
SELECT l.lead_id, a.code as attributeCode, ln.content as noteContent, ln.task_type_id, tt.type_name
FROM leads l JOIN
leads_notes ln
ON ln.lead_id = l.lead_id JOIN
task_types tt
ON tt.task_type_id = ln.task_type_id JOIN
leads_attributes la
ON la.lead_id = l.lead_id JOIN
attributes a
ON a.attribute_id = la.attribute_id
WHERE (l.ambassador=1 OR l.rents_bike=1) AND
(l.city <> '') AND
(l.address <>'' OR l.address2 <>'') AND
(l.country_id <> '') AND
(la.attribute_id IN ($attributes_id)) AND
(l.lead_id = $lead_id);
You have add leads_notes table double: one in after leads table and another in inner join, so remove one solve your problem like following
SELECT leads.lead_id,
attributes.code AS attributeCode,
leads_notes.content AS noteContent,
leads_notes.task_type_id,
task_types.type_name
FROM leads
INNER JOIN task_types
ON task_types.task_type_id = leads_notes.task_type_id
INNER JOIN leads_attributes
ON leads_attributes.lead_id = leads.lead_id
INNER JOIN attributes
ON attributes.attribute_id = leads_attributes.attribute_id
INNER JOIN leads_notes
ON leads_notes.lead_id = leads.lead_id
WHERE ( leads.ambassador = 1
OR leads.rents_bike = 1 )
AND ( leads.city <> '' )
AND ( leads.address <> ''
OR leads.address2 <> '' )
AND ( leads.country_id <> '' )
AND ( leads_attributes.attribute_id IN ( $attributes_id ) )
AND ( leads.lead_id = $lead_id )

Need to fetch last dates from the query mentioned below

I have latest dates of 2017 in attandance date but its show just 2016 dates kindly plz help me
SELECT
added_on,
types. `type`,
student.`student_id`, student.`roll`, student.`class_id`, student.`name` AS student_name,
SUM(datewise_attandance.`total_classes`) AS SumOfTotal,
datewise_attandance.attandance_date AS attandance_date,
SUM(datewise_attandance.`status`) AS SumOfPresent,
#ROUND(((SUM(datewise_attandance.`status`)/SUM(datewise_attandance.`total_classes`))*100),2) AS percentage,
class.`name` AS class_name, student.`sex`, student.`father_name`, student.`address`,
student.`phone`, subject.`name` AS subject_name,
#types.`type`, types.`type_id`,
subject.`subject_id`,
#MAX( datewise_attandance.`date_to` ) AS MaxOfAtt_Date_To,
student.`session_id`
FROM
(( class
INNER JOIN `subject` ON class.`class_id` = subject.`class_id` )
INNER JOIN datewise_attandance # ( INNER JOIN types ON datewise_attandance.`type_id` = types.`type_id` )
ON ( subject.`subject_id` = datewise_attandance.`subject_id` )
AND ( class.`class_id` = datewise_attandance.`class_id` ))
INNER JOIN `types` ON( types.`type_id` = subject.`subject_type`)
INNER JOIN student ON ( student.`student_id` = datewise_attandance.`std_id` )
AND ( class.`class_id` = student.`class_id` )
GROUP BY student.`student_id`,
student.`roll`, student.`class_id`, student.`name`, class.`name`,
student.`sex`, student.`father_name`, student.`address`,
student.`phone`, subject.`name`,
# types.`type`, types.`type_id`,
subject.`subject_id`, student.`session_id`
HAVING (
(
student.`class_id` = 4
AND student.`roll` = 388
AND student.`session_id` = 5
)
) ORDER BY IFNULL(subject.final_exam,0) DESC
You are using inner join so if there are matching records then only result will be returned for 2017. Why dont you use left join and try the same

MYSQL Update AND SUM

i am try to do somethink like this
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`From` FROM `Trans`
) B ON C.`UserID` = B.`From`
SET `Bal` = A.`LSC` - B.`LSC`;
and this is not workink :\
when i just do 1 inner join and run the query twice once to A and second to be it work but i want to do it in one query ...
UPDATE BA C
INNER JOIN
(
SELECT SUM(`Amount`) AS `LSC`,`To` FROM `Trans`
) A ON C.`UserID` = A.`To`
SET `Bal` = A.`LSC`; // Work

Using JOIN sql query

I would like to use JOIN instead of IN in the following SQL query. I can't figure out how to do it.
SELECT * FROM shop_orders WHERE
id IN (SELECT orders_id FROM shop_orders_data WHERE closed='1' /*AND backorder='0'*/ AND exhibition_id='389' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE
country_id IN (SELECT id FROM countries WHERE id='72')) AND in_country = '72' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE start<=1336946400 AND end>1336600800)) AND
id IN (SELECT orders_id FROM shop_orders_products WHERE
products_id IN (SELECT id FROM shop_products WHERE artno='120000' OR name LIKE '%120000%')) AND created>=1333231200 AND created<1333663200 ORDER BY created DESC
I tried this:
SELECT
s.*
FROM
shop_orders s
INNER JOIN shop_orders_data od ON s.id=od.orders_id
INNER JOIN shop_exhibitions se ON od.exhibition_id=se.id
INNER JOIN countries co ON se.country_id=co.id
INNER JOIN shop_orders_products sop ON s.id=sop.orders_id
INNER JOIN shop_products sp
ON sop.products_id=sp.id
WHERE od.closed=1
AND ( sp.artno='120000' or sp.name LIKE '%120000%' )
AND ( od.exhibition_id='389')
AND ( od.in_country = '72')
AND ( se.start <=1336946400)
AND ( se.end >1336600800)
AND ( se.created>=1333231200)
AND ( se.created<1333663200)
ORDER BY `s`.`created` DESC
I this correct??
See if this works (and study the code to learn how it works):
SELECT *
FROM shop_orders so
JOIN shop_orders_data sod ON (
(so.id = sod.orders_id)
AND (sod.closed = '1')
/*AND (sod.backorder = '0') */
AND (sod.exhibition_id = '389')
AND (sod.in_country = '72')
)
JOIN shop_exhibitions se ON (
(sod.exhibition_id = se.id)
AND (se.start <= 1336946400)
AND (se.end > 1336600800)
)
JOIN countries c ON (
(se.country_id = c.id)
AND (c.id = '72')
)
JOIN shop_orders_products sop ON (
(so.id = sop.orders_id)
)
JOIN shop_products sp ON (
(sop.products_id = sp.id)
AND ((sp.artno='120000') OR (sp.name LIKE '%120000%'))
)
WHERE (so.created >= 1333231200) AND (so.created < 1333663200)
ORDER BY so.created DESC;
The join syntax works like this:
SELECT field1,field2,field3
FROM FirstTable
JOIN SecondTable ON (FirstTable.PrimaryKey = SecondTable.ForeignKey)
JOIN ThirdTable ON (FirstTable.PrimaryKey = ThirdTable.ForeignKey)
Try applying this approach to your query.