tags:

views:

284

answers:

2

Hi all,
I've spent a few hours staring at this piece of code. Fresh eyes please! Here is a shortened version of the query:

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 
'FROM (`requests` c) JOIN `inventory` d ON `d`.`listing_seq_no` = `c' at line 7


SELECT DISTINCT `c`.`req_id`, `u`.`user_id`, `u`.`org_name`, 
CONCAT_WS(' ', `l`.`strength`, `l`.`unit)` as dos, `c`.`quantity` AS quantity1, 
    (SELECT sum(quantity) from inventory d2 
          WHERE d2.listing_seq_no = c.listing_seq_no 
    ) as inv_total,
 FROM (`requests` c)   
 JOIN `inventory` d 
    ON `d`.`listing_seq_no` = `c`.`listing_seq_no` 
 JOIN `listings` l 
    ON `l`.`listing_seq_no` = `c`.`listing_seq_no` 

EDIT: Original CodeIgniter Code snippet:

$this->db->select ( "c.req_id,
        u.user_id,
        u.org_name,
        l.tradename as name,
        CONCAT_WS(' ', l.strength, l.unit) as dos,
    );
+1  A: 

try removing the parens around (requests c)

ggiroux
Same as the comment I posted above
Michael
Michaels comment to my answer (that I deleted): I don't know how to remove the parenthesis, as the original function call was done using CodeIgniter as the framework as: $this->db->from('requests c') ..
Peter Lang
I think Max S. just nailed it.
ggiroux
Thx guys for looking at this. I've pasted the CI code that generates the Mysql code -- Not sure how to control the parentheses ... The reason I showed only mysql code before is because I thought I could backtrack the issue easier that way...
Michael
yuck. looks like a bug in the CI 'db' class - you could try removing the 'l' aliases inside the concat_ws() to see if it goes away, or use a plain concat(), or adding the backticks yourself in the code.
ggiroux
+4  A: 

This:

CONCAT_WS(' ', `l`.`strength`, `l`.`unit)`

Should be:

CONCAT_WS(' ', `l`.`strength`, `l`.`unit`)
Max Shawabkeh
Interesting... not exactly sure why that is occurring (generated from Codeigniter)
Michael
I haven't used CodeIgniter, but my guess would be that a space between `unit` and the bracket might be a solution.
Max Shawabkeh
wow. that was the solution. lol.
Michael