I'm learning C# and some SQL server, and now i am trying to get information from my small database.
I have two tables: Movie and MovieHandler.
In the table Movie, i have MovieCodeLable, which is a uniqe number, and the Title of the movie.
In the MovieHandler table, i have MovieCodeLable which is the same as in my Movie table, and i have also her the colum InStore, which is eighter 0 or 1 for not in store or in store.
I'm trying to display the Title for the movies which is not in the store. But i find it hard figure out how to join tables.
I have tried this SQL query:
SELECT Title
FROM Movie
JOIN MovieCodeLable
ON MovieHandler.MovieCodeLable
WHERE InStore = 0
Since i only get errors trying this query in Visual Studio 2012, i've probably missed something fundametal with SQL and JOINS, but i hope that someonw could make JOINS smooth as butter for me and others, struggeling to learn it.
Your JOIN is wrong and your ON clause is incomplete. The JOIN should involve the names of the 2 tables that you are joining, which in this case is Movie and MovieHandler The ON should be expression of format A = B. So your query should be:
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You need to specify both JOIN fields
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You have to do the query like this
SELECT Title
FROM Movie
JOIN MovieHandler
ON Movie.MovieCodeLable = MovieHandler.MovieCodeLable
WHERE InStore = 0
You had to complete the ON condition.You need to specify the columns to match after ON condition.Go to this link there is explanations with tables http://www.w3schools.com/sql/sql_join.asp
I do it this way in oracle. Not sure if it will work for you...
Try:
Select Mv.Title
,other_columns
from Movie Mv
,MovieHandler Mvh
Where Mv.MovieCodeLable = Mvh.MovieCodeLable
and (whatever other criteria you want
As long as MovieCodeLable is unique you're ok
Other_columns can be any column of either table format with table.column syntax (I use table shortcuts Mv and Mvh to reference the tables, I don't know if this is legal for non-oracle)
Related
Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!
Sorry if the title is a bit confusing... I'm trying to fix some data. Here goes the explanation:
Say I have two tables.
The first is a lookup table that is no longer in use.
The second has one varchar(50) column that sometimes has product names and
sometimes has product ids from the old lookup table.
The idea is to convert all the PID values in the Product table into the product names. Here's a pic i made up to help illustrate it:
The lookup table is much larger, that's just an example. So i'd guess it would be an update statement that would use the ProductsLookup.Name value if the Product value was found to be in the ProductsLookup.PID set? How would this look in SQL?
Thanks much for the help,
Carlos
You need to put inner join with both these tables "ProductsLookup" and "Products" on PID and Product column. But Product column in "Products" table is varchar so you have to apply ISNUMERIC function on this column to make sure that join works fine. below is example
UPDATE
P
SET
P.Product=PL.Name
FROM
Products P
INNER JOIN ProductsLookup PL ON PL.PID=CASE WHEN ISNUMERIC(P.Product)=1 THEN P.Product ELSE -1 END
You can do this with an update and join:
update p
set product = pl.name
from products p join
productslookup pl
on pl.pid = p.id;
A left join is not needed, because you only need to update the values that match in the lookup table.
I will caution you that performance might be an issue. The problem is the types between the two tables -- presumably pid is a number not a string. If so, you can create a computed column to change the type and an index on that column.
i have 3 tables: reservation (with foreign key showing_id_showing), showing (with column "date" and foreign key film_id_film) and film (with column "title").
i need to show date together with film title in one column.
separately it will look like this, but i don't know how to put it together.
(SELECT date FROM showing WHERE id_showing=showing_id_showing) "Date",
(SELECT title FROM film WHERE id_film=film_id_film) "Title",
To get the result set from multiple tables in SQL Server you can you SQL JOIN command. Below is similar example using INNER JOIN:
SELECT S.Date,T.Title FROM Showing S
Inner Join Title
on T.ID_Film = T.Film_ID_Film
Where S.ID_Showing=S.Showing_ID_Showing
The actual solution may vary as per your table schema definition.
SELECT film.title || ' # ' || showing.date AS title_and_date
FROM reservation
INNER JOIN showing
ON showing.id_showing = reservation.showing_id_showing
INNER JOIN film
ON film.id_film = reservation.film_id_film
WHERE reservation.id_reservation = :THE_RESERVATION_I_WANT_TO_SHOW
This is assuming you wanted to concatenate to get "one column".
Also assuming you wanted to show the one reservation. Otherwise just drop the last line with the WHERE
I prepared an sqlFiddle, located here: http://sqlfiddle.com/#!2/951c1/2/1
I have 3 tables (cars, colors, and car_color_history). In colors there are the colors with some kind of metadata I am interested in. In cars, there are a bunch of cars with a 'wanted_color' key which tells the car to change to the given color. In car_color_history, there are all the entries, on what specific time a car had what color.
What I want now is a view/select statement, to get all cars with the current and the wanted color information.
My first idea was to do this with left joins and some kind of GROUP BY magic, but up until now, I could not get that to work
A working solution is the second select in the fiddle with subselects. Am I correct that that is not a pretty solution as it is slow? Plus i do have to provide the subselect for each displayed column :(
Anybody have a solution?
SQL Fiddle
select
ca.carkey,
description,
cow.other_info as wanted_color,
cco.other_info as current_color,
cch.datetime as current_color_datetime
from
cars ca
left join
colors cow on ca.wanted_color = cow.colorkey
left join (
select distinct on (carkey) *
from car_color_history
order by carkey, datetime desc
) cch using(carkey)
left join
colors cco on cch.colorkey = cco.colorkey
I have a table B with cids and cities. I also have a table C that has these cids with extra information. I want to list all the cids in table C that are associated with ALL appearances of a given city in Table B.
My current solution relies on counting the number of times the given city appears in Table B and selecting only the cids that appear that many times. I don't know all the SQL syntax yet, but is there a way to select for this kind of pattern?
My current solution:
SELECT Agents.aid
FROM Agents, Customers, Orders
WHERE (Customers.city='Duluth')
AND (Agents.aid = Orders.aid)
AND (Customers.cid = Orders.cid)
GROUP BY Agents.aid
HAVING count(Agents.aid) > 1
It only works because I know right now with the HAVING statement.
Thanks for the help. I wasn't sure how to google this problem, since it's pretty specific.
EDIT: I'm pinpointing my problem a bit. I need to know how to determine if EVERY row in a table has a certain value for a field. Declaring a variable and counting the rows in a sub-selection and filtering out my results by IDs that appear that many times works, but It's really ugly.
There HAS to be a way to do this without explicitly count()ing rows. I hope.
Not an answer to your question, but a general improvement.
I'd recommend using JOIN syntax to join your tables together.
This would change your query to be:
SELECT Agents.aid
FROM Agents
INNER JOIN Orders
ON Agents.aid = Orders.aid
INNER JOIN Customers
ON Customers.cid = Orders.cid
WHERE Customers.city='Duluth'
GROUP BY Agents.aid
HAVING count(Agents.aid) > 1
What variant of SQL are you using?
To start with, you can (and should) use JOIN instead of doing it in the WHERE clause, e.g.,
select Agents.aid
from Agents
join Orders on Agents.aid = Orders.aid
join Customers on Customers.cid = Orders.cid
where Customers.city = 'Duluth'
group by Agents.aid
having count(Agents.aid) > 1
After that, I'm afraid I might be a little lost. Using the table names in your example query, what (in English, not pseudocode) are you trying to retrieve? For example, I think your sample query is retrieving the PK for all Agents that have been involved in at least 2 Orders involving Customers in Duluth.
Also, some table definitions for Agents, Orders, and Customers might help (then again, they might be irrelevant).
I'm not sure if I understood you problem, but I think the following query is what you want:
SELECT *
FROM customers b
INNER JOIN orders c USING (cid)
WHERE b.city = 'Duluth'
AND NOT EXISTS (SELECT 1
FROM customers b2
WHERE b2.city = b.city
AND b2.cid <> cid);
Probably you will need some indexes on these columns.