I have a mysql query using for search script and i need to add another field (like SPV.term alias) This code is tell me an Error Number: 1054 Unknown column 'S.id' in 'on clause'.
$query = "
SELECT
SP.url,
SP.id,
S.name,
SP.hit,
SP.hot,
SP.action,
SP.id,
SP.smallimage,
SP.mainmodimage,
(SELECT
stock
FROM shop_product_variants
WHERE shop_product_variants.product_id = S.id
AND stock > 0
LIMIT 1) as stock,
(SELECT
price
FROM shop_product_variants
WHERE shop_product_variants.product_id = S.id
OR shop_product_variants.product_id = S.id
AND stock = 0
LIMIT 1) as price,
(SELECT
id
FROM shop_product_variants
WHERE shop_product_variants.product_id = S.id
OR shop_product_variants.product_id = S.id
AND stock = 0
LIMIT 1) as v_id,
(SELECT
old_price
FROM shop_product_variants
WHERE shop_product_variants.product_id = S.id
AND stock > 0
LIMIT 1) as old_price
FROM shop_products_i18n S,
shop_product_variants SPV
INNER JOIN shop_products SP
ON SP.id = S.id
WHERE SP.active = 1
AND S.name LIKE '%" . $get . "%'
OR SP.url LIKE '%" . $get . "%'
OR SPV.term LIKE '%" . $get . "%'
GROUP BY S.id
ORDER BY stock DESC
";
This is your from clause:
FROM shop_products_i18n S,
shop_product_variants SPV INNER JOIN
shop_products SP
ON SP.id = S.id
The problem is that you are mixing old-style joins and new-style joins. A simple rule: Never use a commas in the from clause.
I think you really mean:
FROM shop_products_i18n S INNER JOIN
shop_products SP
ON SP.id = S.id
Because you don't use SPV anywhere else in the outer query, except for the where clause. That condition should probably go into each of the subqueries.
By the way, you can fix the original problem by replacing the , with cross join:
FROM shop_products_i18n S CROSS JOIN
shop_product_variants SPV INNER JOIN
shop_products SP
ON SP.id = S.id
Although , and cross join both perform cartesian products, they behave differently in the FROM clause in terms of precedence. The problem that you have with the , is that the precedence rules say the following INNER JOIN is parsed first -- so the columns in S are not available.
Related
I am trying to list the records which meets my condition. As ii am using AND and OR operator together i am not getting the exact report. Here is my query
SELECT o.sales_order_id AS SID, o.reference, o.status, o.last_modified, sol.sales_order_id, sol.item, sol.quantity, sol.selling_price, sol.discount, sol.tax, SUM(sol.tax_amount) AS Tamt, SUM(sol.total) as Total, i.iid, GROUP_CONCAT(DISTINCT i.name) AS iname, l.company, t.tax_id, t.name as tname, t.rate from orders o INNER JOIN before_order_line_items sol ON o.sales_order_id = sol.sales_order_id INNER JOIN leads l ON o.company_id=l.id INNER JOIN items i ON sol.item=i.iid INNER JOIN taxes t ON sol.tax=t.tax_id WHERE o.order_quote='Order' AND o.authorise='Yes' OR o.assigned_to=6 OR o.user_id=6 GROUP BY o.sales_order_id ORDER BY o.sales_order_id DESC
I am storing both orders and quotations in single table Orders, for orders i store it as Order in order_quote column, for Quotations it is Quote
It is not checking order_quote='Order' condition, it displays both orders and quotations.
if i remove OR o.assigned_to=6 OR o.user_id=6 , it gives proper result.
I tried using DISTINCT like this
SELECT DISTINCT o.order_quote=`Order`, .....
But does't work.
UPDATED
SELECT o.sales_order_id AS SID, o.reference, o.status, o.last_modified, sol.sales_order_id, sol.item, sol.quantity, sol.selling_price, sol.discount, sol.tax, SUM(sol.tax_amount) AS Tamt, SUM(sol.total) as Total, i.iid, GROUP_CONCAT(DISTINCT i.name) AS iname, l.company, t.tax_id, t.name as tname, t.rate from orders o INNER JOIN before_order_line_items sol ON o.sales_order_id = sol.sales_order_id INNER JOIN leads l ON o.company_id=l.id INNER JOIN items i ON sol.item=i.iid INNER JOIN taxes t ON sol.tax=t.tax_id WHERE (o.order_quote='Order' AND o.authorise='Yes') AND (o.assigned_to=6 OR o.user_id=6) GROUP BY o.sales_order_id ORDER BY o.sales_order_id DESC
You need to use parentheses. I'm not sure exactly how, but your current where clause is interpreted as:
WHERE (o.order_quote = 'Order' AND o.authorise = 'Yes') OR
(o.assigned_to = 6) OR
(o.user_id = 6)
I would guess that you want something like this:
WHERE (o.order_quote = 'Order' AND o.authorise = 'Yes') AND
(o.assigned_to = 6 OR o.user_id = 6)
But that is mere speculation.
Or perhaps:
WHERE (o.order_quote = 'Order' AND
(o.authorise = 'Yes' OR o.assigned_to = 6 OR o.user_id = 6)
Here is my SQL query which joins 4 tables and it works correctly.
SELECT pl.lms_id, u.id, REPLACE(trim(u.`url`), 'www.', '') AS url, trim(u.`name`) as name, p.date_removed, p.status, p.ignore_status FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u on p.profile_id = u.id INNER JOIN adl_tw_profile_acc_type ac on p.profile_id = ac.profile_id LEFT JOIN `adl_lms_prof_list` pl on u.id = pl.profile_id WHERE u.`vpg_id`='2' AND u.`status` = 'Y' and ac.acc_type_id = '2' ORDER BY u.`url` ASC, p.id DESC
I am facing an issue that, the table adl_seo_status has multiple entries for a single profile_id. So, that accounts are repeating in my listing. I want that account as a single row which means the distinct value of accounts based on profile_id.
You need to use group by, for example:
SELECT
pl.lms_id,
u.id,
REPLACE(trim(u.`url`), 'www.', '') AS url,
trim(u.`name`) AS name,
p.date_removed,
p.status,
p.ignore_status
FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u ON p.profile_id = u.id
INNER JOIN adl_tw_profile_acc_type ac ON p.profile_id = ac.profile_id
LEFT JOIN `adl_lms_prof_list` pl ON u.id = pl.profile_id
WHERE u.`vpg_id` = '2' AND u.`status` = 'Y' AND ac.acc_type_id = '2'
ORDER BY u.`url` ASC, p.id DESC GROUP BY p.profile_id;
Hello I am using the following SELECT
"SELECT
P.id
,P.price
,P.contract
,P.property_type
,CT.id
,CT.english_text
,PT.id
,PT.english
,C.cityname
,S.sectorname
,S.id
FROM properties P
JOIN contract CT
ON CT.id = P.contract
JOIN property_type PT
ON PT.id = P.property_type
JOIN city C
ON C.id = P.city
JOIN sector S
ON S.id = P.sector WHERE 1"
It occur that in the final result I always have id=2, which I think is the id from S.id since it is the last one.
How I can get in foreach loop of this query to display P.id inside $row['id'] ?
Right now is always $row['2'] on each result.
You have multiple columns with the same name, which confuses php. Use aliases to give them different names:
SELECT P.id as p_id, P.price, P.contract, P.property_type,
CT.id as c_id, CT.english_text,
PT.id as pt_id, PT.english
C.cityname,
S.sectorname, S.id as s_id
. . .
This removes all ids from the result set. What you want is called s_id.
I am trying to grab the max date from the updates column but also return the corresponding fullname from that table, what is currently happening is that the latest updates.date is being returned but the first updates.consultant is currently being returned as well and we need the correct fullname for the MAX date.
SELECT customer.id,
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
members.fullname,
Max(updates.`date`) AS MaxDate
FROM updates
LEFT JOIN members
ON members.id = updates.consultant
GROUP BY updates.clientid
ORDER BY updates.date DESC) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3'
I think the easiest way in your case is to use the substring_index()/group_concat() method:
SELECT customer.id,
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
substring_index(group_concat(m.fullname order by u.date desc separator '|'), '|', 1) as full_name
Max(updates.`date`) AS MaxDate
FROM updates u
LEFT JOIN members m
ON m.id = u.consultant
GROUP BY u.clientid
) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3' ;
I have got this query which works fine:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height,
p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users ) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 300 );
Now I am trying to trim the result:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width,
p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users
WHERE account_id = 0) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 3 );
But the second query result is exactly the same as the first one.
Please could somebody shine some light on this?
//UPDATE
I removed the php vaiables to stop the unnecessary confusion :)
give this a try
"SELECT p.design_id, p.product_id, t.account_id,
p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN users AS t
ON p.user_id = t.user_ID AND
t.account_id = $ind
ORDER BY p.design_id LIMIT " . $from . ", " . $howMany
Why don't you try using a placeholder in the where condition too as like below or was it a typo.
"SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users WHERE account_id = **".$ind."**) AS t
USING(user_id) ORDER BY p.design_id LIMIT ".$from.", ".$howMany );
You have not selected column user_id from designs. Your JOIN is not working at all. From the manual:
The USING(column_list) clause names a list of columns that must exist in both tables.
Or all the users have account_id = 0 (or at least all the users that have designs).
If it's neither of those, try removing the word 'AS' before your nested select table 't'.
You could also try it without USING, use "ON t.user_id = p.user_id" instead of "USING(user_id)"