SQL 1064 Error, Syntax going unseen - mysql

SELECT
i.*,
ii.file_location
FROM group_shop_item i, group_shop_itemimage ii, group_shop_brand b
WHERE
i.brand_id = b.id
AND
b.brand_status_id = 1
AND
i.is_deleted = 0
AND
i.is_displayed = 1
AND
i.id = ii.item_id
AND
ii.is_main = 1
AND
i.deal_participate = 1
AND
i.brand_label_id IS NOT NULL
ORDER BY i.datetime_modified DESC;
This SQL query keeps throwing me a 1064. It seems to be on the last line which I've tried with and without the i table variable. I can't for the life of me catch the error, anyone can lend me another pair of eyes?
I'm throwing this as a RAW query into the in built Django function and building this query with string concatenation. This copy paste is directly from a print I've done from the command line. It's outputting neatly but isn't reading when I run the view on my browser.

Over 25 years ago ANSI Standard join syntax was adopted. You need to cease using comas between table names in the from clause.
SELECT
i.*
, ii.file_location
FROM group_shop_item i
INNER JOIN group_shop_itemimage ii ON i.id = ii.item_id
INNER JOIN group_shop_brand b ON i.brand_id = b.id
WHERE i.is_deleted = 0
AND i.is_displayed = 1
AND ii.is_main = 1
AND i.deal_participate = 1
AND i.brand_label_id IS NOT NULL
AND b.brand_status_id = 1
;
Regarding the 1064 error, please read this without the exact error message and the exact/full query we can't offer much insight into that.
The other thing you need to be careful of is that "select *" isn't good practice either.

Related

MS Access UPDATE with JOIN on three conditions

I'm trying to update records through a single inner join with multiple criteria. My best effort so far is this:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON d.ProductCode = g.ProductCode AND
ON d.ProductionLineCode = g.ProductionLineCode AND
ON g.MonthIndex = MONTH(d.SowingDate)
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
Access gives the error 'Syntax error (missing operator)' and highlights the 'r' in 'd.ProductCode'. The join is guaranteed to give a single row.
Could anyone give me pointers on how to fix this?
D'oh. The answer was as follows:
UPDATE FormData d
INNER JOIN ProductGrowthDays g
ON (d.ProductCode = g.ProductCode
AND d.ProductionLineCode = g.ProductionLineCode
AND g.MonthIndex = MONTH(d.SowingDate))
SET d.EstimatedDays = g.GrowingDays
WHERE
d.EventTypeId = 1
I was sure I tried that at one point, but obviously not. Well, leaving this here if someone else should need it.

MySQL SELECT syntax error that I am not able to identify

I am trying a simple query in mysql and I am getting a syntax error that I need help understanding.
SELECT
eea.*,
ee.description,
eect.title,
eect.file,
eect.location,
eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1,
eea.ee_id = ee.id,
ee.eect_id = eect.id;
I am getting the following error:
Basically the syntax error on line 13
eea.ee_id = ee.id, ee.eect_id = eect.id LIMIT 0, 25
Anyone have idea how I can edit this to get the -1 vote to improve?
Its a simple syntax error, your WHERE clause should not be seperated by commas. Use AND or OR etc
SELECT
eea.*, ee.description, eect.title, eect.file,
eect.location,eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1 AND
eea.ee_id = ee.id AND
ee.eect_id = eect.id
LIMIT 0,25
You should also learn about the JOIN syntax
SELECT
eea.*, ee.description, eect.title,eect.file,
eect.location, eect.img_location
FROM `e_exam_attempt` eea
JOIN `e_exam` ee ON eea.ee_id = ee.id
JOIN `e_exam_cert_template` eect ON ee.eect_id = eect.id
WHERE
eea.a_user_id = 1
LIMIT 0,25

unknown class: Fixnum Rails MySQL query - inserting variable breaks my search

I had a query which was working just fine:
#schedule = Schedule.find(params[:id])
#schedule_tasks = ScheduleTask.select("s.*, t.*, t.*, d.*, st.*").from("schedule_tasks st").
joins("left join schedules s ON s.id = st.schedule_id").
joins("left join tasks t ON t.id = st.task_id").
joins("right join days d ON d.id = st.day_id").
order("d.number, t.name").
group_by{|d| d.number}
I had to refine my search to only schedule_tasks with a specific schedule_id, so I edited the second line to:
joins("left join schedules s ON s.id = st.schedule_id AND s.id = ?", #schedule.id).
This has cause the following error:
unknown class: Fixnum
The error goes away if I take out the group_by - but I need that, and I have tried hard coding in the number instead of #schedule.id and that does not work either, a google search does not reveal a lot of details on this error.
For anyone coming here from Google, I used plain string interpolation to fix this issue. This method is vulnerable to SQL Injection, so make sure you type check your variables before using them.
In this case I would do
#schedule_id = #schedule.id
.
.
.
joins("left join schedules s ON s.id = st.schedule_id AND s.id = #{#schedule_id}")
Rather than following learning_to_swim's answer, which as noted is at risk of SQL injection, couldn't you cast your #schedule_id to a string?
#tasks = ScheduleTask.joins("left join [...] s.id = ?", #schedule.id.to_s)

SQL error #1064 when nesting a SELECT MAX in a LEFT JOIN

Please let me know why I get an error on the following SQL (I am using a MySQL database) and how to rectify it:
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
I broke the SQL down to ensure each part worked.
SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b
And
SELECT at_award_description.ad_id,
at_award_description.ad_detail,
at_award_description.ad_archived_date,
a.cad_id,
a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;
Each of these work on their own. When I combine them I get the following error:
Error
SQL query:
LIMIT 0, 25
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 25' at line 1
Regards,
Glyn
EDIT
#Barmar came up with the solution to remove the ";" at the end when testing. However, the result was not what I expected so I ended up changing this to:
SELECT at_award_description.ad_id,
at_award_description.aw_id,
at_award_description.ad_group,
at_award_description.ad_detail,
at_award_description.ad_start_date,
at_award_description.ad_archived_date,
MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group

Unusual SQL Server Query Result

I faced with an unusual query result while running a sql server query.
The important table to note is Code_Structure_Type.
It contains columns Code_Structure_Type_Id smallint, Name char(50) and few other columns that are not used here.
My Query
SELECT DISTINCT A1.A1_Id AS Code_Unit_Attribute_Id,
A1.Name AS Code_Unit_Attribute_Name,
Code_Structure_Type.A1_Name AS Code_Attribute_Type
--, Code_Structure_Type.Code_Structure_Type_Id
FROM Defect
INNER JOIN Defect_Code_Unit
ON Defect.Defect_Id = Defect_Code_Unit.Defect_Id
INNER JOIN Code_Unit
ON Defect_Code_Unit.Code_Unit_Id = Code_Unit.Code_Unit_Id
INNER JOIN A1
ON Code_Unit.A1_Id = A1.A1_Id
INNER JOIN ERA_Module
ON Defect.ERA_Module_Id = ERA_Module.ERA_Module_Id
INNER JOIN Code_Unit_Type
ON Code_Unit.Code_Unit_Type_Id = Code_Unit_Type.Code_Unit_Type_Id
INNER JOIN Code_Structure_Type
ON Code_Unit_Type.Code_Structure_Type_Id = Code_Structure_Type.Code_Structure_Type_Id
WHERE ERA_Module.ERA_Project_Id = 472
AND ERA_Module.Is_Active = 1
AND Defect.Is_Closed = 0
AND Defect_Code_Unit.Is_Active = 1
AND Code_Structure_Type.Name = 'Pega Rules'
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
AND A1.A1_Id > 0
AND Code_Unit.Scope_Id = 1
This query returns me with a single row which is incorrent. The expected result of this query should be none because we do not have Defects for 'Pega Rules' Code_Structure_Type.
If I uncomment this line
--, Code_Structure_Type.Code_Structure_Type_Id
I get no results which is correct.
Also if I filter from the Code_Structure_Type_Id which is for 'Pega Rules' not with Code_Structure_Type.Name I get the correct result as well which is none.
To do this I uncomment this line
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
and comment this line
AND Code_Structure_Type.Name = 'Pega Rules'
So basically I'm doing the same thing and there is no other name like 'Pega Rules' in the table Code_Structure_Type, not even close. Only five rows are there in that table.
I need to understand what is happening here.
Thanks in advance.
Links to Execution Plans
Files, when used 'pega rules' only - incorrect_original.xml when used 'pega rules' + select - correct_with_added_select.xml when used id - correct_with_Id.xml