SQL - how to join data from two tables (adminer) - mysql

I am a complete newbie in sql... I am using w3s tutorial to learn some basic queries to help me at work. I'm trying to join two tables (order info about quantity, price and product ordered and the second one with client data - name, second name, phone number). My database is on Adminer 4.2.4. I wrote something like this (all the columns names are correct):
SELECT order_id.orders_products, quantity.orders_products, product_id.orders_products, firstname.orders_address, lastname.orders_address, phone.orders_address
FROM orders_products
INNER JOIN orders_address ON orders_address.order_id=orders_products.order_id;
but I'm still getting this error:
error in query (1054): unknown column 'order_id.orders_products' in field list
Thank you in advance for any help :)

Hope this will help
SELECT orders_products.order_id,
orders_products.quantity,
orders_products.product_id,
orders_address.firstname,
orders_address.lastname,
orders_address.phone
FROM orders_products
INNER JOIN orders_address ON orders_address.order_id=orders_products.order_id;

Related

Creating three columns in SQL using the following information

I am trying to create a statement that combines the following into three columns, BusinessEntityID, AveQuota and PctComm. I am having trouble joining, I keep getting a syntax error near join. Please help
SELECT BusinessEntityID, AVG (SalesQuota) as AveQuota
from [sales].[SalesPersonQuotaHistory]
group by BusinessEntityID
join
select (commissionpct * salesytd) as PctComm from sales.SalesPerson
The orginal question was to find each sales person's average Quote (from QuotaHistory, using aggregation) as a column called AvgQuota and join it with a column that calucalates the same sales person's commissions earnings YTD (from SalesPerson, uses two colums from a calculation).
You are missing the information on which column to join those tables in the the 'join' syntax. Please refer to join syntax here: https://www.w3schools.com/mysql/mysql_join.asp#:~:text=MySQL%20Joining%20Tables,a%20related%20column%20between%20them.&text=Notice%20that%20the%20%22CustomerID%22%20column,is%20the%20%22CustomerID%22%20column.

I cannot get my joins to work on a some tables, but they will work on others. Trying to select columns from 2 tables

I am new to mySQL so sorry for the basic question,
When I try to join my rental table and tp_rental table via the 'Rental_ID' column I get errors saying Error code: 1052 Column 'Rental_ID' in field list is ambiguous.
i can join rental and member tables no problem.
i am trying to print out a transaction report when a rental is completed (game is returned) that it will show rental ID, memberID, Due Date and Date Returned.
I was using the below code but getting errors:
select Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID
Rental_ID is a column in each of the two tables you are joining so the server does not know which one you want rental.Rental_ID or tp_rental.Rental_ID even though in this particular they both would have the same value. Make which one you want explicit, for example:
select tp_rental.Rental_ID,member_ID,completed,Date_Due,Date_Returned
from gamestoredb.rental
inner join gamestoredb.tp_rental
on rental.Rental_ID=tp_rental.Rental_ID

MySQL Workbench Error 1066

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.

I am not understanding MySQL Joins at all. I'm stuck

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!

inner join with COUNT() and GROUP BY

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