You have an error in your SQL syntax; check the manual - php

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 'WHERE j.id_customer = 1' at line 4
SELECT j.`id_customer`, j.`id_order`, m.`id_shop`
FROM `ps_orders` j
LEFT JOIN `ps_order_detail` m
WHERE j.`id_customer` = 1
this is generated from original code in prestashop php on if detailed error correction is turned on -------
foreach ($result as $key)
{
$customer_id_is = 1;
$product_id_is = 5;
$result2 = Db::getInstance()->executeS(
'SELECT j.`id_customer`, j.`id_order`, m.`id_shop`
FROM `'._DB_PREFIX_.'orders` j
LEFT JOIN `'._DB_PREFIX_.'order_detail` m
WHERE j.`id_customer` = '.$customer_id_is.'
');
}

You are missing the ON clause in your JOIN
SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
LEFT JOIN ps_order_detail m
ON m.SomeField = j.SomeField <-- HERE
WHERE j.id_customer = 1
If you are joining your tables you need to link them on a field. Otherwise you are performing a cross join.
The MySQL Docs state:
In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.
So this would work:
SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
JOIN ps_order_detail m
WHERE j.id_customer = 1
as would this:
SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
INNER JOIN ps_order_detail m
WHERE j.id_customer = 1
But it does not work for OUTER JOINs.

When you link two tables you have to define a column on each table that connection them.
You do that in the ON condition of a JOIN which you did not specify. Example:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.pk_id = table2.fk_id

Related

MySQL Inner Join query giving errors

I have two Tables - char_items and items. item_id is a common field among the two tables.
I want read the item_id from 'char_items' table and use that to obtain other information from the 'items' table based on that item_id. But my query is showing up as incorrect in MySQL. Please help --
SELECT * FROM `char_items` WHERE char_id=$char_id && isSlotted=1 INNER JOIN `items` ON char_items.item_id=items.item_id
I keep getting the message:
#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 'INNER JOIN `items` ON char_items.item_id=items.item_id
LIMIT 0, 30' at line 1
Joins need to happen before the where clause
SELECT *
FROM char_items c
INNER
JOIN items i
ON c.item_id = i.item_id
WHERE char_id = $char_id
AND isSlotted = 1
where should be after join clause as like below.
SELECT * FROM `char_items` INNER JOIN `items` ON char_items.item_id=items.item_id WHERE char_id=$char_id && isSlotted=1;

my sql :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 ''

i have query like this
select c.*,j.pict
from mst020 a
inner join mst022 c on c.mst020_id = a.id
left join (select e.pict as pict from mst021 e
where e.line_number =
(select max(f.line_number) from mst021 f where f.mst020_id = a.id)
and e.mst020_id = a.id) j
but when i process this query,, error show :
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 '' at line 5
i'm trying to learn my sql because at oracle its not problme if g have subquery like that.thx
You're missing an ON statement to go with the rest of the join.
I've reformatted your query:
select c.*,j.pict
from mst020 a
inner join mst022 c on c.mst020_id = a.id
left join (
SELECT e.pict as pict
FROM mst021 e
WHERE e.line_number =
(
SELECT max(f.line_number)
FROM mst021 f
WHERE f.mst020_id = a.id
)
AND e.mst020_id = a.id
) j
As you see, the last left join doesn't have an on clause, and the last and is misplaced. Also, in your inner most query (select max) you refer to the outermost query. This is not possible. You should probably rewrite the query, at least, I don't know what you are trying to do, so I can't correct it.
i have an answer. the error because i'm join the table at sub query and with outside subquery like this
select c.*,j.pict
from mst020 a
inner join mst022 c on c.mst020_id = a.id
left join (select e.pict as pict from mst021 e
where e.line_number =
(select max(f.line_number) from mst021 f where f.mst020_id = a.id)
and **e.mst020_id = a.id**) j on j.mst021_id = a.id
so i change to this and thats successful
select c.*,j.pict
from mst020 a
inner join mst022 c on c.mst020_id = a.id
left join (select e.pict as pict from mst021 e
where e.line_number =
(select max(f.line_number),g.mst021_id from mst021 f )) j on j.mst021_id = a.id
at my sql we can't join with table at outside subquery

If the value inside NOT IN is empty mysql is throwing an error

If m.message_type_id IN () is empty it is giving me an error which says
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 ') ) UNION ( ' at line 16
SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
IF(m.owner_id = 45,'sent','received') as type
FROM messages as m
LEFT JOIN events as e ON m.message_type_id = e.id
LEFT JOIN user_organization as uo ON uo.id = e.org_id
WHERE m.message_type_id IN ()
AND m.type = 'event_invite_request_msg'
and if WHERE m.message_type_id IN (61) it gives me the correct response.
What is the solution?
IN Clause expects you to send at least one value, why would you expect it should work even with an empty parameter?
expr NOT IN (value,...)
Syntax
On a broader level this is the case with almost all comparisons, why would you expect a comparison to not result in an error if there is an invalid value to compare against
Would this work for you?
SELECT myField FROM myTable WHERE MyField2 =
Nope it won't
You'll have to use condition for that
if(count($friendsArray[])){
$qry = "SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
IF(m.owner_id = 45,'sent','received') as type
FROM messages as m
LEFT JOIN events as e ON m.message_type_id = e.id
LEFT JOIN user_organization as uo ON uo.id = e.org_id
WHERE m.message_type_id IN ($ids)
AND m.type = 'event_invite_request_msg'";
}else{
$qry = "SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
IF(m.owner_id = 45,'sent','received') as type
FROM messages as m
LEFT JOIN events as e ON m.message_type_id = e.id
LEFT JOIN user_organization as uo ON uo.id = e.org_id
WHERE m.type = 'event_invite_request_msg'";
}

Codeigniter/PHP Outer JOIN Syntax on Multiple Tables

I am trying to perform an outer JOIN 3 tables based on the "site_id" column in each table. I'm using Codeigniter/Active Record.
The user enters a site number via a form and once the number is submitted it should call the model that executes something like this:
$site_id = $this->input->post('site_id');
$this
->db
->select('*')
->where('site_id', $site_id)
->from('sites')
->join('leader', 'sites.site_id = leader.site_id', 'outer')
->join('state', 'sites.site_id = state.site_id', 'outer');
$q = $this->db->get();
However I get the following error message:
"Error Number: 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 'OUTER JOIN leader ON sites.site_id = leader.site_id OUTE' at line 3
SELECT * FROM (sites) OUTER JOIN leader ON sites.site_id = leader.site_id OUTER JOIN state ON sites.site_id = state.site_id WHERE site_id = '63'
Filename: /home2/cdowney/public_html/mclinbase.com/models/viewsite_model.php
Line Number: 33"
I believe(hope) this is just a small syntax error that I'm missing or a misuse of the outer join but I have not been able to figure it out.
Any guidance?
Try this -
$this->db->select('*');
$this->db->where('sites.site_id', $site_id);
$this->db->from('sites');
$this->db->join('leader', 'sites.site_id = leader.site_id', 'outer');
$this->db->join('state', 'sites.site_id = state.site_id', 'outer')
$q = $this->db->get();
Simply union select the left and right join to get the same result as outer join:
SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.id
UNION
SELECT * FROM table1
RIGHT JOIN table2 ON table1.id = table2.id
Maybe this will help.

Inner Join using COUNT(*)

I am selecting the user's information from my MySQL database as shown below:
SELECT * FROM `Users` WHERE `$user_or_id` = ?
However, I would like to add an extra bit off information to the returned data. The extra bit of data is the total number of records in a table named 'Venues' where the rows' field, 'user_id' is the same as the 'id' field in the table, 'Users'.
Please can you tell me where I am going wrong with the following query? Here is the error I am receiving:
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 Users AS u INNER JOIN Venues AS v ON u.id =
v.user_id WHERE u.id = '' at line 1
SELECT u.*, v.count(*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
SELECT u.*, COUNT(v.*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
COUNT is a MySQL function, not a member of table v. Pass it an argument representing what you want to count-- in this case, v's rows.
It should be COUNT(v.*). Otherwise it's interpreted as "function count inside table V", which isn't valid.
Just use count(*) instead of v.count(*).

Categories