MySQL Workbench Error 1066 - mysql

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.

Related

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

SQL not exists to find some issues

I'm practicing some SQL (new to this),
I have the next tables:
screening_occapancy(idscreening,row,col,idclient)
screening(screeningid,idmovie,idtheater,screening_time)
Im trying to creating a query to search which clients watched all the movies in the "screening" table and show their ID(idclient).
this is what I written(which doesn't work):
select idclient from screening_occapancy p where not exists
(select screeningid from screening where screeningid=p.idscreening)
I know it's probably not that good so please try to explain also what am I doing wrong.
P.S My mission is to use not/exists while doing it...
Thanks!
Your query is basically fine, although the select distinct is unnecessary in the subquery:
select p.idclient
from screening_occapancy p
where not exists (select 1
from screening s
where s.screeningid = p.idscreening
);
Notes:
You can select anything in the exists subquery. Selecting a column is misleading.
Use table aliases and use them for all column references, particularly in a correlated subquery.
If you are designing the tables, I would advise you to give the primary key and foreign key the same name (screeningid or idscreening, but not both).
EDIT:
If you want clients who watched all movies, then I would approach this as:
select p.idclient
from screening_occapancy p
group by p.idclient
having count(distinct p.screening_occapancy p) = (select count(*) from screening);
Why don't you count the number of movies in the screening_table, load it into a variable and check the results of your query results against the variable?
load number of movies into variable (identified by idmovie):
SELECT count(DISTINCT(idmovie)) FROM screening INTO #number_of_movies;
check the results of your query against the variable:
SELECT A.idclient,
count(DISTINCT(idmovie)) AS number_of_movies_watched,
FROM screening_occapancy A
INNER JOIN screening B
ON(A.idscreening = B.screeningid)
GROUP BY A.idclient
HAVING number_of_movies_watched = #number_of_movies ;
If you want to find all clients, that attended all screenings, replace idmovie with screeningid.
Even someone relatively new to MySQL can get his head around this query. The "not exists"-approach is more difficult to understand.

SQL Return All Rows Where 2 Values Are Repeated

I am sure this question has already been answered, but I can't find it or the answer was too complicated. I am new to SQL and am not sure how to word this generically.
I have a mySQL database of software installed on devices. My query to pull all the data has more fields and more joins, but for brevity I just included a few. I need to add another dimension to create a report that lists every case where a device has more than one installation of software from the same product family.
sample
Right now I have code kind of like this and it is not doing what I need. I have seen some info on exists but the examples didn't account for multiple joins so the syntax escapes me. Help?
select
devices.name,
sw_inventory.product,
products.family_name,
sw_inventory.ignore_usage,
from sw_inventory
inner join products
on sw_inventory.product=products.product_name
inner join devices
on sw_inventory.device_name=devices.name
where sw_inventory.ignore=0
group by devices.name, products.family_name
There are plenty of answers out there on this topic but I definitely understand not always knowing terminology. you are looking for how to find duplicates values.
Basically this is a two step process. 1 find the duplicates 2 relate that back to the original records if you want those. Note the second part is optional.
So to literally find all of the duplicates of the query you provided
ADD HAVING COUNT(*) > 1 after group by statements. If you want to know how many duplicates add a calculated column to count them.
select
devices.name,
sw_inventory.product,
products.family_name,
sw_inventory.ignore_usage,
NumberOfDuplicates = COUNT(*)
from sw_inventory
inner join products
on sw_inventory.product=products.product_name
inner join devices
on sw_inventory.device_name=devices.name
where sw_inventory.ignore=0
group by devices.name, products.family_name
HAVING COUNT(*) > 1

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

SQL to Filter on multiple dimensions with a many-to-many relationship

In my rails app I have three tables to deal with the many-to-many relationship between courses and categories
courses
course_categories_courses
course_categories
I have groups of categories, and I want to allow filtering of the listing of the courses by categories through an interface like:
Location
very near
near
far
Type
short
medium
long
To search for medium types either near or far I had thought of using:
SELECT distinct courses.*
FROM `courses`
inner join course_categories on
course_categories_courses.course_category_id = course_categories.id
and (
course_categories.id in ('medium')
and course_categories.id in ('near', 'far')
)
but that's not working. Anyone able to point me in the right direction please?
You generally want to put to 'relationships' as join conditions, and values (literals) to filter by in the where clauses
ex. join condition - on course_categories_courses.course_category_id = course_categories.id
ex. where clause - where course_categories.id in ('near', 'far')
So you might want to rewrite your query that way. But more than that, how can the both of these possible be true?
(course_categories.id in ('medium') and course_categories.id in ('near', 'far'))
Is that what you intend?
You have to join both tables and you are using id field where type and location (according to your description) should be used
SELECT distinct courses.* FROM `courses`
inner course_categories_courses on course_categories_courses.course_category_id = courses.course_category_id
inner join course_categories on
course_categories.id = course_categories_courses.id
where (course_categories.type in ('medium') and course_categories.location in ('near', 'far'))
Syntactaclly the query you wrote is fine. There are two unusal things about it as other's have noted
Filter on a Inner join. (Typically done in the where but perhaps you have your reasons)
course_categories.id in ('medium'). Most would expect a number here but perhaps this is just for demonstration purposes.
The reason for it "not working"
You're getting an error that you've swept under the rug in your ruby that you're not sharing
Its working there simply are no records matching your criteria.
Ways to debug.
Run it in Workbench (or some other client) and check the results
If there are no errors and no record are returned run some queries in workbench
Select count(*) from course_categories where course_categories.id in ('medium')
Select count(*) from course_categories where course_categories.id in ('near', 'far')
If you got the results you wanted in Workbench then its probably your client (Ruby) code, bad connection string perhaps?