I have some SQL (cut down from Entity Framework for readabilty) that works in SQL Server but not MySQL. I am assuming this means I have hit a limitation of MySQL but can not be sure.
I have 3 tables:
products with a ProductId and AccommodationId
accommodations with an AccommodationId
rooms with a RoomId
SELECT *
FROM (SELECT * FROM products) AS Project1
WHERE EXISTS(
SELECT *
FROM rooms
LEFT OUTER JOIN
(SELECT * FROM accommodations AS A WHERE Project1.AccommodationId = A.AccommodationId)
AS Project2 ON 1 = 1
)
I get this error:
Error Code: 1054. Unknown column 'Project1.AccommodationId' in 'where clause'
I would like to understand this error to see if I can change my code to get EntityFramework to produce compatible SQL.
Thanks
You cannot JOIN to a subquery in MYSQL and refer back to one of the other tables in your main FROM clause. That won't work.
Wow, that's some really ugly looking code right there. I think what it boils down to is the following. It just gets there in a REALLY round about way.
SELECT Project1.*
FROM products AS Project1
INNER JOIN accommodations AS A
ON Project1.AccommodationId = A.AccommodationId
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`
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;
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!
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
I'm kind of a MySQL novice and can't figure out what is going wrong here. I have two tables. The left table is called Workouts. The relevant columns are date (type date) and id (type int).
The right table is called Workout_locations (relevant fields: workout_id type int, and location_id type int).
The join fields are Workouts.id and Workout_locations.workout_id.
All I want to do is get a table of two columns: date (from Workouts), and location_id (from Workout_locations). I need to only pull records from the Workouts table based on a couple of fields (the sql statement should make this clear).
Here is my sql syntax:
SELECT Workouts.date as date, Workout_locations.location_id as loc_id
FROM Workouts
WHERE Workouts.pacegroup_id='9' AND (Workouts.date BETWEEN '2013-08-19' AND '2013-08-25')
INNER JOIN Workout_locations ON Workouts.id=Workout_locations.workout_id"
But I get this error:
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 Workout_locations ON
Workouts.id=Workout_locations.workout_id' at line 1
I'm hoping that this is a very easy error to spot for someone who is experienced with this. Can anyone see it? Thanks!
Your INNER JOIN should come before the WHERE. I also don't think you needed the parens around your BETWEEN clause, but I doubt that it'll cause an error either way:
SELECT Workouts.date as date, Workout_locations.location_id as loc_id
FROM Workouts
INNER JOIN Workout_locations ON Workouts.id=Workout_locations.workout_id
WHERE Workouts.pacegroup_id = '9'
AND Workouts.date BETWEEN '2013-08-19' AND '2013-08-25';
Also, though they technically let you get away with it, you should avoid using "date" as a selected column name (it's a reserved word).
You could do a bit of streamlining as well to make things a bit easier to read:
SELECT Workouts.date AS wo_date, Workout_locations.location_id AS loc_id
FROM Workouts w
INNER JOIN Workout_locations l ON w.id = l.workout_id
WHERE w.pacegroup_id = '9'
AND w.date BETWEEN '2013-08-19' AND '2013-08-25';