I am using phpMyAdmin to test this query but keep getting a syntax error. I've tried looking it up in the MySql manual and trying other syntactical possibilities but I've gotten older in this process. Thanks for your help
SELECT image_title, image_id, COUNT(other_sales.*) FROM art
INNER JOIN other_sales ON (art.image_id=other_sales.image_id)
GROUP BY (other_sales.image_id);
MySQL said: Documentation
Documentation
1052 - Column 'image_id' in field list is ambiguous
Ultimately, I want to count the number of times a specific number (image_id) occurs in the 'other_sales' table
To troubleshoot these 1064 errors:
The error message gives a snippet of your query. The first character of the snippet is the first character the MySQL interpreter could not understand.
So in the case of your query, it's
SELECT image_title, image_id, COUNT(other_sales.*) FROM art INNER JOIN ...
ggggggggggggggggggggggggggggggggggggggggggggggggbbbbbbbbbbbbbbbbbbbbbbbbbbb
where g means good and b means bad.
Your actual problem: you can't put more than one value in a COUNT() function. You tried to put COUNT(something.*) which makes no sense to count.
Notice that COUNT(*) is a special case meaning just count the rows.
the tables art and other sales probably both have the column image_id.
specify the table before the column like art.imageid or asign a alias to the table and then to the column like so
SELECT o.image_title, o.image_id, COUNT(*)
FROM art a JOIN other_sales o ON (art.id=other_sales.image_id)
GROUP BY (o.image_id)
Yes, I see that now. There are 2 problems with my original code.
I should have chosen the specific column name instead of using *
image_id is a column name that appears in both tables I am accessing. That will cause an "ambiguous" error.
Below is the original code and the corrected code:
original:
SELECT image_title, image_id, COUNT(other_sales.*) FROM art INNER JOIN other_sales ON (art.image_id=other_sales.image_id) GROUP BY (other_sales.image_id);
corrected:
SELECT image_title, art.image_id, COUNT(other_sales.image_id) FROM art INNER JOIN other_sales ON (art.image_id=other_sales.image_id) GROUP BY (other_sales.image_id);
thanks for the help
Related
Hello the wonderful community,
I have a basic config MySQL / PHP with this DB :
Pages Table
Tags Table
Pages_Tags Table
Query :
SELECT *, GROUP_CONCAT('tags_name')
FROM pages
LEFT JOIN pages_tags ON pages_tags.pages_id = pages.pages_id
LEFT JOIN tags ON tags.tags_id = pages_tags.tags_id
GROUP BY pages_tags.pages_id;
and i have the following error :
"Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column this is incompatible with sql_mode=only_full_group_by"
So i made a lot of researches, and seems there are 2 solutions :
1/ To change the SQL mode
2/ To have the same column in the SELECT & GROUP BY.
Both seems bad solutions. The first because i don't want to change the default config and the second is insane, i need a lot of columns in the SELECT and not only the GROUP BY. Especially if the query is more complicated with a lot more LEFT JOIN. I need to display all theses infos.
Do you have any solutions ? Alternative with other methods ?
I'm open to anything ;)
Thanks a lot !!
starting from mysql 5.7 you can use column in select not involved in aggreagtion function and not mentioned in group by clause, so you should not use * (all) for column but add explict column name in select and mention the column in select not involved in aggregation function in group by
SELECT `pages_tags`.`pages_id`, GROUP_CONCAT('tags_name')
FROM `pages`
LEFT JOIN `pages_tags` ON `pages_tags`.`pages_id` = `pages`.`pages_id`
LEFT JOIN `tags` ON `tags`.`tags_id` = `pages_tags`.`tags_id`
GROUP BY `pages_tags`.`pages_id`
Could someone please tell explain to me how to properly process a query that collects information from 2 tables, i thought I had this figured out until I added more records. Please look at the image I have below:enter image description here
(The last record should not have the name "Thomas Murray" in it)
Then there is the query I am processing:
"select a.*, b.forenames, b.surname FROM playerSkills a, playerdb b GROUP BY sheetNo"
What I was hoping to do is collect all from the playerSkills database (which it does) and only bring over the names from the second database (playerdb) that matched with the playerID but as I want to return more than one result so I don't know what to do as it returns the whole column and just pastes the one name into every field.
Though I am sure a JOIN is to be inserted here, I am not sure which or at all.
I am not experienced with SQL but trying to wrap my head around it. I have experimented with the JOIN clauses but didn't get far probably due to a syntax.
How can join the names to the playerID so they appear in the appropriate fields?
You need columns to join on . . . and proper join syntax:
select ps.*, p.forenames, p.surname
FROM playerSkills ps JOIN
playerdb p
ON ps.playerId = p.playerId;
Notes:
Your query does not require GROUP BY.
Your query does require JOIN conditions.
Kudos for using table aliases. They should be abbreviations for the table name.
You want to always use explicit JOIN syntax. No commas in the FROM clause.
I want to join two tables together which are the run table and the restaurant table. From the run table I need the run_id and the restaurant_id and from the restaurant table, I need the created_date so in the end, I have a table that contains the created_date of each restaurant. Both the run and restaurant_history tables have run_id's so that's how I know I can join them. I came up with something like:
SELECT run_id, restaurant_id, created_date FROM restaurant_history, run
JOIN run ON restaurant_history.run_id = run.run_id;
But that gave me an error. Any help would be appreciated :)
(I'm fairly new to sql)
You are mixing both implicit joins and explicit joins. The implicit join syntax (listing the tables in the FROM clause) was deprecated over 25 years ago.
As a simple rule, never use commas in the FROM clause:
SELECT R.run_id, restaurant_id, created_date
FROM restaurant_history H
JOIN run R ON H.run_id = R.run_id;
As for why it gave you that error, error is two-fold. Let's look at what you had written:
SELECT run_id, restaurant_id, created_date
FROM restaurant_history, run
JOIN run ON restaurant_history.run_id = run.run_id;
The query you had before was the equivalent of the following:
SELECT run_id, restaurant_id, created_date
FROM restaurant_history
CROSS JOIN run
INNER JOIN run ON restaurant_history.run_id = run.run_id;
The reason for the error was because you had the table run listed twice in the query with no aliases to discern between the two. The ON clause referenced the run table, but it didn't know which one you meant.
Additionally, you're unintentionally creating a CROSS JOIN between restaurant_history and run - something I'm sure you don't want.
But just removing the second table from the FROM clause will still give you an error about an ambiguous column (run_id) in your SELECT statement. This column exists in both tables (as we can see from the JOIN), and without explicitly telling it which table to select from, it doesn't know how to handle the column and will throw an error.
To fix this, you will also need to alias the tables (the H and R aliases I've put in the solution).
For more information on different JOIN types, see this question:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
For more information on Explicit vs Implicit JOINs, see here:
Bad habits to kick : using old-style JOINs
SELECT run_id, restaurant_id, created_date
FROM restaurant_history H
INNER JOIN run R ON H.run_id = R.run_id
Try this query
SELECT run_id, restaurant_id, created_date
FROM restaurant_history
INNER JOIN run ON restaurant_history.restaurant_id= run.run_id;
The following is what I'm working on for one of my query joins, I'm trying to create a table where the general screening details and combined with movie details. I keep getting the error 1066. Can anyone help or elaborate as to why?
SELECT screening.ScreeningID, screening.MovieID, SeatCapacity, SeatsAvailable
FROM screening
LEFT JOIN movie
ON screening.ID = movie.ScreeningID
UNION
SELECT MovieName, Director, Genre
FROM movie
RIGHT JOIN movie
ON screening.ID = movie.ScreeningID;
UPDATE:
Thanks guys for the replies, I have been an absolute complete noob and over complicated things (as usual). After some more digging around, this is what I came up with which perfectly works. I was trying to output the seating availability in a screening in one table by combining data from multiple tables.
SELECT
s1.ScreeningID,
s1.MovieID,
s1.SeatCapacity,
s1.SeatsAvailable,
m1.Director,
m1.MovieName,
m1.Genre
FROM screening s1, movie m1
WHERE m1.screeningID = s1.screeningID
You need to alias your tables and also each query that participates in the UNION must have the same number of columns.
Note: UNION is two complete (mostly unrelated) SELECT queries.
Your second query:
SELECT MovieName, Director, Genre
FROM movie
RIGHT JOIN movie
ON screening.ID = movie.ScreeningID;
Doesn't even have a table or alias called screening. (As well as column counts not matching for the UNION) It also has the table movie included twice, so every single column you access is going to be ambiguous and generate an error unless you use table aliases e.g.: FROM movie AS m1 etc.
I recommend starting by getting each separate query working by itself, only then combine them with a UNION statement.
So I'm trying to learn some MySQL and I have gotten to a point on Joins. It is doing my head in.
I have a few exercises I am trying to work through and getting no where.
The current exercise calls for this:
Use the pre-1994 SQL syntax (i.e. do not use the INNER JOIN syntax) to display the order date, order number and the shipper company name for orders shipped to
Portugal. Sort the output in ascending order of order date.
I currently have typed this:
SELECT OrderDate, OrderID, ShipperID FROM Shippers Orders
WHERE shippers.shipperID = orders.shipperID AND orders.shipcountry = 'Portugal';
But I am getting this error message:
Error Code: 1054. Unknown column 'OrderDate' in 'field list'
What am I doing wrong? Also, what can I provide to help you guys to help me?
OrderDate DOES exist, as you can see here from my screenshot here of 'Select * from Orders'
So I imagine that is similar but with a different join process.
Thank you in advance!
You have to use table name with column names to identify that which column belongs to which table.
SELECT orders.OrderDate, orders.OrderID,shippers.ShipperID FROM Shippers, Orders
WHERE shippers.shipperID = orders.shipperID AND orders.shipcountry = 'Portugal';
Or you can you aliasing that will be easy to write the query.
SELECT b.OrderDate, b.OrderID, a.ShipperID FROM Shippers a, Orders b
WHERE a.shipperID = b.shipperID AND b.shipcountry = 'Portugal';
Hope this will help.
You can use like this.
SELECT o.OrderDate, o.OrderID, s.ShipperID FROM Shippers s, Orders o
WHERE s.shipperID = o.shipperID AND o.shipcountry = 'Portugal';
Use the pre-1994 SQL syntax
Sounds like the training course is taking you the long way around (though the date is wrong; probably should be 1989)!
The lesson here is that doing a relational product operation between two tables (a.k.a. CROSS JOIN to use the 1992 term, which remains outside core Standard SQL) forces you to use SQL's dot qualified references in the form <range variable reference>.<column reference> in order to disambiguate what would otherwise be duplicated column references (e.g. in your case where shipperID appears in both tables).
I guess the ultimate destination is NATURAL JOIN, where the need for range variables is dispensed with entirely!