SQL: Column is ambigious - php

I have the following SQL query, which has a subquery in it:
SELECT * FROM `statics` WHERE `mmsi`=
(SELECT `mmsi` FROM `positions`,`active`
WHERE `active.mmsi` = `positions.position_ID`);
But when I execute it, I get the following error:
1052 - Column 'mmsi' in field list is ambiguous
Please help me on adjusting my query.

Without seeing your table structure, this is a wild guess:
SELECT *
FROM `statics`
WHERE `statics.mmsi` = (SELECT `active.mmsi`
FROM `positions`,`active`
WHERE `active.mmsi` = `positions.position_ID`);
but I don't get why are you doing this with a subquery. This one should yield the same results
SELECT statics.*
FROM `statics`, `positions`
WHERE `statics.mmsi` = `positions.position_ID`;

Related

Query Builder in Laravel (GROUP BY)

$bo_facilities = DB::select('SELECT
a.bo_facility_code,
a.bo_facility_groupcode,
a.bo_number,
a.bo_indYesOrNo,
a.bo_indLogic,
b.bo_content_facility_description
FROM bo_facilities AS a
RIGHT JOIN bo_content_facilities AS b
ON b.bo_content_facility_code = a.bo_facility_code AND b.bo_content_facility_facilityGroupCode = a.bo_facility_groupcode
WHERE a.bo_hotel_code = "'.$hotel['code'].'" GROUP BY b.bo_content_facility_description');
Error message:
"message": "SQLSTATE[42000]: Syntax error or access violation: 1055
'philex.main.a.bo_facility_code' isn't in GROUP BY (SQL:
SELECT a.bo_facility_code,
a.bo_facility_groupcode,
a.bo_number,
a.bo_indyesorno,
a.bo_indlogic,
b.bo_content_facility_description
FROM bo_facilities AS a
right join bo_content_facilities AS b
ON b.bo_content_facility_code = a.bo_facility_code
AND b.bo_content_facility_facilitygroupcode =
a.bo_facility_groupcode
WHERE a.bo_hotel_code = "'.$hotel['code'].'"
GROUP BY b.bo_content_facility_description
)",
I was getting this error because of the GROUP BY method.
Take a look at the aggregate functions in mysql.
If you group your results by b.bo_content_facility_description the others fields in the SELECT that are not in the GROUP BY should be aggregated with a function like SUM, COUNT, etc.
Grouping by that single field without any arregation on the other fields is meaningful, because you are not specifying how to combine the other fields to get the single record for the grouped field.
I suggest you to clear your head about what results you expect.

Mysql query returns error #1093 - You can't specify target table

I need to check and update with same query my database.
The error says it is not possble to update same table which is included in the select statement. Is there any workaround of this to happen in 1 mysql query? Here is the query:
$query='update option_values_to_products set available="0" where id in (
select ovtp.id from option_values ov,option_values_to_products ovtp,options o where
ovtp.product_id="1657" and ovtp.option_values_id=ov.id and ov.options_id=o.id and
o.name="Size" group by ovtp.id )';
Yes, this is a nagging feature of mysql and there is a workaround to it: wrap the subquery within the IN() clause into another subquery.
update option_values_to_products set available="0" where id in (select id from (
select ovtp.id from option_values ov,option_values_to_products ovtp,options o where
ovtp.product_id="1657" and ovtp.option_values_id=ov.id and ov.options_id=o.id and
o.name="Size" group by ovtp.id ) as t)
Avoid using nested queries for many reasons like performance and memory issue, also it can be very hard to be understood for the next developers
Good practice Split your query into 2 parts :
<?php
$qSelect = 'select ovtp.id from option_values ov,option_values_to_products ovtp,options o where
ovtp.product_id="1657" and ovtp.option_values_id=ov.id and ov.options_id=o.id and
o.name="Size" group by ovtp.id';
$res = DATABASE_MANAGER::exec($qSelect);
$qUpdate = 'update option_values_to_products set available="0" where id in (' . implode(",", $res) .')';
$res2 = DATABASE_MANAGER::exec($qUpdate);

Mysql query to select records where the sum of a column's values is in a given variable range

I've been trying to write a query to select all records from a table where the sum of a column's values is between a variable range.
Here's what I came up with so far:
$result = mysql_query(' SELECT * FROM table WHERE SUM(column) BETWEEN $Range1 AND $Range2 ORDER BY RAND());
However when I try to loop through the above query with the mysql_fetch_object function it gives me a common error (The supplied argument is not a valid result).I've tried different ways of writing it but still come up short
So I tried the query using aliases you guys provided but still get the same error.
$result = mysql_query(' SELECT column1, SUM(column2) AS Total FROM table GROUP BY column1 HAVING Total BETWEEN $Range1 AND $Range11 ORDER BY RAND()');
I'm not sure what the "ordering by rand" is needed for but your final query will look something like this:
SELECT *, SUM(column) AS `total`
FROM table
GROUP BY someColumn
HAVING `total` BETWEEN $Range1 AND $Range2
ORDER BY RAND()
;

PHP & MYSQL: How to resolve ambiguous column names in subquery

After trying to query the following nested query
SELECT ur.userID, us.fullname
FROM tbl_user_recipe AS ur JOIN tbl_user_settings AS us ON ur.userID = us.userID
WHERE relationship = 'analyzed' AND userID IN
( SELECT ux.userID
FROM tbl_user_recipe AS ux
WHERE ux.relationship = 'collected'
);
I'm getting the following, and idea why?
#1052 - Column 'userID' in IN/ALL/ANY subquery is ambiguous
You need to prefix the alias to the table here:
WHERE relationship = 'analyzed' AND ur.userID IN

Query based on enum value

I am trying to query based on the value of an enum field. Essentially I have 3 values (categories) for the enum field, a, b and c respectively. I am attempting to populate a select field based on the results of the following query:
SELECT * WHERE pid = $id AND group = 'a';
I have tried this with and without single quotes and get the error You have an error in your SQL syntax; and have narrowed it down to being a lack of knowing how to query based on a specific enum value. I assumed this would work and see no reason for it not to so if I can be enlightened I would greatly appreciate it.
Group is a reserved keyword use it like this
SELECT * FROM mytable WHERE pid = $id AND `group` = 'a';
Use tablename
SELECT * From tablename WHERE pid = $id AND `group` = 'a';
Try This..
SELECT * FROM tbname WHERE pid = $id AND `group` = 'a';

Categories