Column ambiguous in mysql? - mysql

I was running this query for the last 6 months with no problems on several servers, but today got an error?
error_message: Database query failed: Column 'account_id' in field list is ambiguous
Column account_id is ambiguous?
$sql_query = "SELECT pricenotifier_criteria.criteria_id , pricenotifier_criteria.`event_id` , pricenotifier_criteria.`ticket_id` , pricenotifier_criteria.`criteria` ,pricenotifier_criteria.account_id ,seller_ids.user_id,seller_ids.seller_id
FROM pricenotifier_criteria
INNER JOIN seller_ids
ON ( pricenotifier_criteria.account_id = seller_ids.account_id )
INNER JOIN pricenotifier_users
ON (pricenotifier_criteria.user_id = pricenotifier_users.user_id )
INNER JOIN pricenotifier_pricing_table
ON ( pricenotifier_users.pricing_id = pricenotifier_pricing_table.pricing_id )
WHERE status=1 AND pricenotifier_pricing_table.processing_time = 15
AND pricenotifier_criteria.user_id = 339
ORDER BY pricenotifier_criteria.event_id
LIMIT 2000
OFFSET 0";
I have tried to run this query in sql in phpmyadmin but no error came there ? if i run this via php than i got that error
APOLOGY TO BE MADE
I am really sorry in the page where i was running this query i had called another page where other programmer didnt mentioned table prefrix that's why we got this error but all of the guys who replied was right so everyone has almost true answer about (why ambigius error comes). I posted it here because i was sure my query has nothing ambigious and i was right but the other file was running some query and had that error.

Cleaner version of the same thing...
SELECT c.criteria_id
, c.event_id
, c.ticket_id
, c.criteria
, c.account_id
, s.user_id
, s.seller_id
FROM pricenotifier_criteria c
JOIN seller_ids s
ON s.account_id = c.account_id
JOIN pricenotifier_users u
ON u.user_id = c.user_id
JOIN pricenotifier_pricing_table g
ON g.pricing_id = u.pricing_id
WHERE status = 1
AND g.processing_time = 15
AND c.user_id = 339
ORDER
BY c.event_id
LIMIT 2000;

I bet your php code just selects account_id without table prefix pricenotifier_criteria or seller_ids. Since both tables contain a field account_id this would be ambiguous without table prefixes.

This error :
error_message: Database query failed: Column 'account_id' in field list is ambiguous
means that account_id is the field of 2 or more tables. As an example :
SELECT account_id, other_things
FROM table_account
INNER JOIN table_account2 ON (table_account.account_id = table_account2.account_id)
account_id is the field of 2 tables : table_account and table_account2. Then, this is ambiguous, because from which table will the account_id supposed to be selected ?
As #strawberry states, the only field that can be ambiguous is status in the query you showed us. If you have really this problem, make sure you prefix all your fields in your query like this : table.field_name. Then, this will avoid this kind of error.

Related

Inner join in combination with sum and group by

I am having some strange issue with the SQL statement below. The result groups by user IDs and some of them turn out right but for one of them (user ID = 1) the "initial_average" is multiplied by 3. I really have no idea why.. Is there something wrong with the structure of the statement? If it is not clear, the aim is to sum the field "initial_avg" in the "tasks" table and have it broken out by user. Some help with this is much appreciated. I am using MySQL.
SELECT sum(initial_avg) AS initial_average
, sum(initial_std) AS initial_standard_dev
, tasks.user
, hourly_rate
FROM tasks
INNER JOIN user_project
ON tasks.user=user_project.user
AND tasks.project=59
AND tasks.user=1
GROUP BY tasks.user
I just solved it by adding another "and" clause (AND user_project.project=59 )
Optimize your query (Try it):
SELECT SUM(initial_avg) AS initial_average, SUM(initial_std) AS initial_standard_dev, tasks.user, hourly_rate FROM tasks INNER JOIN user_project ON tasks.user = user_project.user AND tasks.project = User_project.project WHERE tasks.project = 59 AND tasks.user = 1 GROUP BY tasks.user, hourly_rate

Retrieving data from one table while comparing value from another table

Overview
I have two tables as can be seen below:
user_planes
----------------------------------
|id |user_id|plane_id|fuel|status|
----------------------------------
| 2 1 1 1 Ready |
----------------------------------
shop_planes
------------------------
|id |name|fuel_capacity|
------------------------
| 1 bob 3 |
------------------------
Foreign Key Primary Key
user_planes.plane_id <-> shop_planes.id
I want to be able to get every field (SELECT *) in user_planes and name and fuel_capacity based on the following criteria:
WHERE user_planes.user_id = ? - Parameter which will be added to the query through PHP.
WHERE user_planes.status = 'Ready'
WHERE user_planes.fuel < shop_planes.fuel_capacity
The Issue and My Attempts
I've tried JOIN however it retrieves data which doesn't fit that criteria, meaning it gets extra data which is from shop_planes and not user_planes.
SELECT * FROM `user_planes` WHERE fuel IN (SELECT shop_planes.fuel_capacity FROM shop_planes WHERE fuel < shop_planes.fuel_capacity) AND user_planes.user_id = 1 AND status = 'Ready'
and
SELECT * FROM `user_planes` INNER JOIN `shop_planes` ON user_planes.fuel < shop_planes.fuel_capacity AND user_planes.user_id = 1 AND user_planes.status = 'Ready'
I've searched Stackoverflow and looked through many questions but I've not been able to figure it.
I've looked up many tutorials but still can't get the desired result.
The desired result is that the query should use the data stored in user_planes to retrieve data from shop_planes while at the same time not getting any excess data from shop_planes.
Disclaimer
I really struggle using JOIN queries, I could use multiple separate queries however I wish to optimise my queries hence I'm trying to bring it in to one query.
If their isn't clarity in the question, please do say, I'll update it to the best of my ability.
Note - Is there an easy query builder option available either through phpmyadmin or an alternative software?
Thanks in advance.
Your last attempt was not a bad one, the only thing you missed there was the join criteria you described at the beginning of your post. I also moved the other filters to the where clause to better distinguish between join condition and the filters.
SELECT `user_planes`.*
FROM `user_planes`
INNER JOIN `shop_planes` ON user_planes.plane_id = shop_planes.id
WHERE user_planes.fuel < shop_planes.fuel_capacity AND user_planes.user_id = 1 AND user_planes.status = 'Ready'
First you need the base JOIN
SELECT up.* -- only user_plane fields
FROM shop_planes sp -- CREATE alias for table or field
JOIN user_planes up
ON sp.id = up.plane_id
Case 1: apply a filter in where condition with php parameter.
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE up.user_id = ?
Case 2: apply a filter in where condition with string constant
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE user_planes.status = 'Ready'
Case 3: aply filter comparing fields from both tables
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE up.fuel < sp.fuel_capacity
Try something like:
SELECT
up.id AS User_Plane_ID
, up.[user_id]
, up.plane_id
, up.fuel
, up.[status]
, sp.name AS shop_Plane_Name
, sp.fuel_capacity AS shop_Plane_Fuel_Capacity
FROM User_Planes up
INNER JOIN Shop_Planes sp ON up.plane_id = sp.id
AND up.fuel < sp.Fuel_Capacity
WHERE up.[status] = 'Ready'
AND up.[user_id] = ?
Definitely find a tutorial for JOINs, and don't use SELECT *. With SELECT *, you may end up querying much more than you actually need and it can cause problems if the table changes. You'll enjoy your day much more if you explicitly name the columns you want in your query.
I've aliased some of the columns (with AS) since some of those column names may be reserved words. I've also moved the JOIN criteria to include a filter on fuel

MYSQL Query - Checking result is true/false and count

I am trying to check if the current user is already following the selected user, and I am doing this like so:
(I know it's not the best way, but as I am new to MYSQL this is as much as I have been able to come up with)
SELECT EXISTS (SELECT 1 FROM Activity WHERE IdOtherUser = 86 AND id = 145)
I am '145' and the user I selected is '86'.
Now that return '0' If I am not following and '1' If I am following that person.
Seems to be working already but it definetly needs improving!
Now what I would like to do is count the followers in the same query.
So count the people I am following and the people following me.
'Activity' is the table where I store the followers and I save them like this:
'id' = me
'idOtherUser' = other user I followed
'type' = type of action "follow"
I have done count's before when calculating the like counts, but I just cannot get my head around this!!
If anyone could spare some time to help me it is much appreciated!
I am sorry if the question is not the best, but I am still learning and trying my best to format them as clear as possible to understand.
Thanks in advance!!
If you trying to count the followers from specific id from table Activity you might do this way:
SELECT COUNT(idOtherUser) AS "I Follow",
(SELECT COUNT(idOtherUser) FROM Activity WHERE idOtherUser = 145 AND type = "follow"
) AS "FOLLOW ME",
(SELECT COALESCE(id,0) FROM Activity WHERE IdOtherUser = 86 AND id = 145 AND type = "follow")
FROM Activity WHERE id = 145 AND type = "follow"
you can use a "correlated subquery" to simplify the query and you might want distinct in the count also (depends on you data). I would avoid using spaces in column aliases too.
SELECT
COUNT(DISTINCT A1.idOtherUser) as i_follow
, (
SELECT
COUNT(DISTINCT A2.id)
FROM Activity A2
WHERE A2.idOtherUser = A1.id
AND A2.type = 'follow'
) as following_me
FROM Activity A1
WHERE A1.id = 145
AND A1.idOtherUser = 86
AND A1.type = 'follow'
Try it with distinct then without, if the result is the same leave distinct out of the query.

SQL Querying multiple tables throwing syntax error?

I was hoping someone could help me figure out what I am doing wrong with a SQL query I am trying to run from within Sequel Pro. The error message 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 '.id
INNER JOIN directory_person ON directory_twitter.id = directory_person.id
WH' at line 1
And the query that I have written is
SELECT directory_twitter.id, directory_twitter.name, directory_twitter.screen_name,
directory_twitter.followers, directory_group.id, directory_group.title
FROM directory_twitter, directory_group
INNER JOIN directory_person_groups
ON directory_person_groups.person_id = directory_twitter.id,
directory_person_groups.group_id = directory_group.id
INNER JOIN directory_person
ON directory_twitter.id = directory_person.id
WHERE directory_person.appears_in_directory = "1"
I am trying to get the query to return the name of the users (directory_twitter.name), their screen name (directory_twitter.screen_name), their total number of followers (directory_twitter.followers), and the name of the group they are in (directory_group.title). Unfortunately the way our tables are set up, the only way I can think to join the group to the user is by INNER JOIN-ing a third table (directory_person_groups) where the group ID and the user ID are both present.
Finally, the only users that I want to be returned by this query are those who are in our directory (WHERE directory_person.appears_in_directory = "1"). In the table directory_person, directory_person.id = directory_twitter.id.
I have been trying to figure out what my error is for hours and I've made no progress. Is everything correct except a syntax error that I am unfamiliar with? Any and help is greatly appreciated. Thank you!
EDIT: All of the columns of the tables that I'm querying are below.
directory_twitter: id, person_id (which is the same value as id), screen_name, name, followers, user_id (I'm not sure where else this selector is used in the database, it is a different value from id and person_id).
directory_group: id (different from directory_twitter.id), slug(a slug of the title), title, screen_name (the Twitter handle of the group, e.g. CNN for #cnn)
directory_person_groups: id (I'm not sure where else if anywhere this value appears in the database, it is different from both directory_twitter.id and directory_group.id), person_id, group_id
directory_person: id (the same as directory_twitter.id which is the same as directory_twitter.person_id), title (this value is different from directory_group.title), first_name, last_name, appears_in_directory
Try this query but you need to add some indexes or it will take a very long time to run. I would suggest you make the columns in WHERE clause as indexes before I run it if I were you.
SELECT DT.name, DT.screen_name, DT.followers, DG.title
FROM directory_twitter AS DT, directory_group AS DG, directory_person_groups AS DPG
WHERE DPG.person_id=DT.person_id AND DPG.group_id=DG.id AND DT.person_id IN
( SELECT DP.id
FROM directory_person AS DP
WHERE appears_in_directory='1'
) ;
Try this
SELECT DIRECTORY_TWITTER.ID,
DIRECTORY_TWITTER.NAME,
DIRECTORY_TWITTER.SCREEN_NAME,
DIRECTORY_TWITTER.FOLLOWERS,
DIRECTORY_GROUP.ID,
DIRECTORY_GROUP.TITLE
FROM DIRECTORY_TWITTER,
DIRECTORY_PERSON_GROUPS,
DIRECTORY_PERSON,
DIRECTORY_GROUP
INNER JOIN DIRECTORY_PERSON_GROUPS
ON DIRECTORY_PERSON_GROUPS.PERSON_ID = DIRECTORY_TWITTER.ID
AND DIRECTORY_PERSON_GROUPS.GROUP_ID = DIRECTORY_GROUP.ID
INNER JOIN DIRECTORY_PERSON
ON DIRECTORY_TWITTER.ID = DIRECTORY_PERSON.ID
WHERE DIRECTORY_PERSON.APPEARS_IN_DIRECTORY = "1"
I changed the comma to AND

Run A Query and Update the Results in MySql

Please forgive the lack of formatting!!!
For some reason I can't figure out how to make it show up
pretty like everyone elses...
Long time roamer, first time poster. I'm a newb to MySql and I'm having trouble understanding why the following code doesn't work.
UPDATE CUSTOMER_T
SET Cust_Status = 'INACTIVE'
WHERE (SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL));
What I'm trying to do is find anyone whose status is ACTIVE AND their bill is more than 14 days late
OR they are ACTIVE AND they have a NULL status for their Billing_Date_Paid field.
Then when I find them, I want to UPDATE their Cust_Status to be "INACTIVE".
If I simply run the following:
SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL);
Then I can pinpoint the people I'm looking for. However, whenever I try to incorporate this code into an UPDATE statment (the first bit of code I posted) I get the following error:
Error Code: 1241. Operand should contain 1 column(s)
You can actually do a JOIN in the update like so:
UPDATE CUSTOMER_T a
INNER JOIN BILLING_T b ON
a.Cust_No = b.Cust_No AND
a.Cust_Status = 'ACTIVE' AND
(
b.Billing_Date_Paid IS NULL OR
DATEDIFF(b.Billing_Due_Date, b.Billing_Date_Paid) < -14
)
SET a.Cust_Status = 'INACTIVE'