MySQL SELECT syntax error that I am not able to identify - mysql

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

Related

SQL 1064 Error, Syntax going unseen

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.

MySQL : error in your SQL syntax | LIMIT 0, 25

I dont understand this error, guys please help me. why I am getting this error..
Is there something wrong in my query?
this is the error..
#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 7
and this is my query:
SELECT
equivalent
FROM
tb_student_record
INNER JOIN
tb_student ON tb_student_record.stud_id = tb_student.stud_id
WHERE
tb_student_record.instructor_id = 'INST-20131296'
AND tb_student_record.criteria_id = '1'
AND tb_student_record.class_record_id = '1'
AND (CONCAT(stud_fname, ' ', stud_lname) = 'Jeffrey Oliveras'
AND tb_student_record.term = 'Prelim'
you are missing closing paranthesis at concat function
SELECT equivalent FROM tb_student_record INNER JOIN tb_student ON tb_student_record.stud_id=tb_student.stud_id
WHERE tb_student_record.instructor_id = 'INST-20131296'
AND tb_student_record.criteria_id = '1'
AND tb_student_record.class_record_id = '1'
AND (CONCAT(stud_fname, ' ', stud_lname) = 'Jeffrey Oliveras' )
AND tb_student_record.term = 'Prelim'
It seems that your query have problem at the end:

Error code 1093:Error in Sql Syntax

I have the following sql .
update voting_table
Set VOTING_STATUS = 1
where CE_ID = 15813
and
VOTING_PK =
(SELECT VOTING_PK FROM voting_table
ORDER BY VOTING_PK DESC
LIMIT 1) ;
But the editor shows me the following error :
Error code 1093, SQL state HY000: You can't specify target table 'voting_table' for update in FROM clause
How can I overcome the error ? Please help me .
I think these SQL will helpful to you.
update voting_table as table_1, (SELECT VOTING_PK FROM voting_table ORDER BY VOTING_PK DESC LIMIT 1) as table_2
where table_1.VOTING_PK = table_2.VOTING_PK and CE_ID = 15813
Set VOTING_STATUS = 1
Thank you.

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

Getting the closest time in a PDO statement

I am working from 2 databases and I need to find records which matches the closest times. Both fields are datetime().
So in essence:
table1.time = 2012-06-07 15:30:00
table2.time = 2012-06-07 15:30:01
table2.time = 2012-06-07 15:30:02
table2.time = 2012-06-07 15:30:03
NOTE: The table I am querying (table2) is a mssql table, and table1.time is a datetime() time. I need to find in table2 the row which closest matches table1.time, but I have no guarnatee that it would be an exact match, so I need the closest. I only need to return 1 result.
I tried the SQL below based on an example from a previous stackoverflow query but it failed to work.
Table1 is a mysql database where table2 is mssql and the query happens on table2 (mssql)
try {
$sql = "
SELECT
PCO_AGENT.NAME,
PCO_INBOUNDLOG.LOGIN AS LOGINID,
PCO_INBOUNDLOG.PHONE AS CALLERID,
PCO_INBOUNDLOG.STATION AS EXTEN,
PCO_INBOUNDLOG.TALKTIME AS CALLLENGTH,
PCO_INBOUNDLOG.CHANNELRECORDID AS RECORDINGID,
PCO_SOFTPHONECALLLOG.RDATE,
PCO_INBOUNDLOG.RDATE AS INBOUNDDATE
FROM
PCO_INBOUNDLOG
INNER JOIN
PCO_LOGINAGENT ON PCO_INBOUNDLOG.LOGIN = PCO_LOGINAGENT.LOGIN
INNER JOIN
PCO_SOFTPHONECALLLOG ON PCO_INBOUNDLOG.ID = PCO_SOFTPHONECALLLOG.CONTACTID
INNER JOIN
PCO_AGENT ON PCO_LOGINAGENT.AGENTID = PCO_AGENT.ID
WHERE
PCO_INBOUNDLOG.STATION = :extension
AND ABS(DATEDIFF(:start,PCO_SOFTPHONECALLLOG.RDATE))
";
$arr = array(":extension" => $array['extension'], ":start" => $array['start']);
$query = $this->mssql->prepare($sql);
$query->execute($arr);
$row = $query->fetchAll(PDO::FETCH_ASSOC);
$this->pre($row);
}
I am getting the following error at the moment:
SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]
Found a shorter version:
SELECT * FROM `table` WHERE `date` < '$var' ORDER BY date LIMIT 1;