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';
Related
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 am trying display all the relevant information for jobs done to a customer using a JOIN TABLE QUERY.
Here is my code...
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice
LEFT OUTER JOIN Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID
I keep getting this error...
'#1064 - 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 '.JobID WHERE Job.CustomerID = Customers.CustomerID' at line 3'
The problem is that you are trying to access tables that you haven't told the query about in both your WHERE and SELECT clauses. When you say Bill.BillID in your SELECT clause, you're saying that you want the Field BillID from the table called Bill.
The rule your syntax is breaking is that you can't use a table name in the SELECT or WHERE clauses, unless you've mentioned it in the FROM clause (or a JOIN).
So now, let's look at your query.
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice
LEFT OUTER JOIN Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID
This means that you want to pick fields from the tables named Bill and Duty, and filter the results based on fields from the Job and Customer tables. However, you've only mentioned one table by name in the FROM clause (Invoice). You've almost joined the Duty table, but you're joining it to a place in a table you haven't mentioned yet (Bill). I'm going to guess that you intend to use Bill as an alias for Invoice. That would mean that what you really want is more like:
// Still incorrect
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice AS Bill
LEFT OUTER JOIN Duty ON Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID
But we still haven't mentioned either the Job or Customer tables that are referenced in the WHERE clause. In order to use them, you need to JOIN those tables as well. We'd need to know more about your schema to figure out how to do that, but I can tell you that you'll need at least two more JOIN clauses. Assuming that you have a CustomerID field on the Invoice table, we probably want to join like this.
// speculative
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice AS Bill
LEFT OUTER JOIN Duty ON Duty.JobID = Bill.JobID
JOIN Customer ON Customer.CustomerID = Bill.CustomerID
JOIN Job ON // Well, something...We don't know.
I strongly recommend you spend some time reading and studying the excellent Wikipedia entry on SQL JOINs. Good luck~
Your mistake is how you point your tables, maybe:
FROM Invoice as Bill LEFT OUTER JOIN NameTable2 as Duty ON Dury.JobID=Bill.JobID
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