473,472 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Quary Confusion

13 New Member
I have the following tables and fields.

Actors
-ActorID
-LastName
-FirstName

Movies
-MovieID
-MovieName
-Year
.
.
.

Genres
-GenreID
-Genre

Ratings
-RatingID
-Rating

MoviesActors
-MovieActorID
-MovieID
-ActorID

I figured out how to do the joins to make it display the movie with all its actors. How would I go about making it show one field with the actors first and last name without changing the table though?

Thanks!!
May 10 '09 #1
4 1647
Uncle Dickie
67 New Member
You should be able to concatenate the fields with something like:

Expand|Select|Wrap|Line Numbers
  1. SELECT FirstName + ' ' + LastName
  2.  
May 11 '09 #2
FooFighter
13 New Member
Oh geesh that was easy.

Thank you very much for your assistance.

Another thing I'm trying to accomplish is this...

I've been trying to figure out how to use the DISTINCT function. Say I'm searching for all movies I have with Will Smith. If I have 3 of the main actors listed under each movie it lists them each 3 times. Here's the query I'm using to list the movies.

Expand|Select|Wrap|Line Numbers
  1. SELECT     TOP (100) PERCENT dbo.Movies.MovieName AS [Movie Name], dbo.Movies.Year, dbo.Movies.RunningTime AS [Running Time], 
  2.                       dbo.Actors.LastName + ', ' + dbo.Actors.FirstName AS [Actor Name], dbo.Genres.Genre, dbo.Ratings.Rating, dbo.Movies.Description
  3. FROM         dbo.Ratings INNER JOIN
  4.                       dbo.Movies INNER JOIN
  5.                       dbo.Genres ON dbo.Movies.GenreID = dbo.Genres.GenreID ON dbo.Ratings.RatingID = dbo.Movies.RatingID INNER JOIN
  6.                       dbo.MoviesActors INNER JOIN
  7.                       dbo.Actors ON dbo.MoviesActors.ActorID = dbo.Actors.ActorID ON dbo.Movies.MovieID = dbo.MoviesActors.MovieID
  8. ORDER BY 'Movie Name', [Actor Name]
Trying to use DISTINCT for the Movies table, but can't get it figured out err.
May 11 '09 #3
Uncle Dickie
67 New Member
I think you would be better using a WHERE clause rather than DISTINCT to list all films that have a particular actor as:
Expand|Select|Wrap|Line Numbers
  1. Bad Boys, Smith, Will
  2. Bad Boys, Lawrence, Martin
  3. Bad Boys, Leoni, Tea
  4.  
are 3 DISTINCT entries so will still show up.

Try something like:

Expand|Select|Wrap|Line Numbers
  1. SELECT m.MovieName
  2.        ,m.Year
  3.        ,m.RunningTime
  4.        ,g.Genre
  5.        ,r.Rating
  6.        ,m.Description
  7. FROM   dbo.Movies m
  8. JOIN   dbo.Genres g ON g.GenreID = m.GenreID
  9. JOIN   dbo.Ratings r ON r.RatingID = m.RatingID
  10. JOIN   dbo.MoviesActors ma ON ma.MovieID = m.MovieID
  11. JOIN   dbo.Actors a ON a.ActorID = ma.ActorID
  12. WHERE  a.LastName = 'Smith'
  13.   AND  a.FirstName = 'Will'
  14.  
If you knew the ActorID rather than text, searching you query would be quicker (but I don't know how you are using your database).

I have also used table aliases in the above code as IMHO it makes the query not only quicker to write but easier to read!

Hope this helps
May 11 '09 #4
FooFighter
13 New Member
Actually I am going to use the ActorID as the search instead of the name. Just wanted to see where I was going wrong on the DISTINCT thing. Using WHERE does sound a lot more sensible though. Thanks for the direction. Also thanks for the info on the aliases. That does indeed looks like it'll make things look cleaner.
May 11 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Doug Farrell | last post by:
Hi all, I'm trying to do the following from within a code module: import re # text to match text = "Good morning x something /x, how are you today x something else /x"
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
0
by: i_have_control | last post by:
I'd be grateful for any input on this one: I have three web domains. The destinations of two are set to folders on the first, though that fact is transparent to the user (i.e: it does not...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
2
by: Riaaaa | last post by:
Hello, We are doing the project in VB.Net. We had a great confusion for ASP.Net and VB.Net. Is VB.Net project performed in Microsoft Visual Studio 2005 ?? We have...
4
by: mumbai10 | last post by:
oracle quary I want to transfer data from ms excel to oracle or how can i import data from ms excel to oracle
2
by: dreamer247 | last post by:
hii.. I have written a quary in sqlserver 2000 and the out put is not as expected... the quary is.. $sql="SELECT NAME,ROOM,DESIG,EMAIL,PHONE,START_DATE,END_DATE,CHAIRED_BY FROM CRB_MEETING...
4
by: ahmed222too | last post by:
when i use this quary to add new column, the engine reject the table name and focus on (-) in the table name alter table AH10-A1*1*1-101006 add column sss note: this schema of table name...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.