0

I'm in a intro to database class, and one of my queries is seriously giving me trouble. The assignment says to: Write a query to display the Passenger name, Seat No and Destination. Display this in one column title Travellers_info. This column should display data in the following format “ Mary Ann Jenkins is assigned to Seat 15 on the way to Bellmead”

This is the relationship view: http://prntscr.com/1jsoay

Can somebody please help me out, I'm not sure where I've gone wrong.

SELECT passenger.name + 'is assigned to Seat'
     + seat_info.seat_no + 'on the way to'
     + departure_info.destination AS Travellers_info
FROM passenger, seat_info, departure_info, seat_passenger, manages
WHERE passenger.Pass_id=seat_passenger.pass_id
  AND seat_passenger.Seat_id=seat_info.Seat_id
  AND seat_info.seat_id=manages.Seat_id
  AND manages.Dept_id=departure_info.dept_id
Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109

5 Answers5

1

You cant "add" text values using the "+" operator.

Without knowing which database you are using, the solution is probably either using CONCAT():

SELECT concat(passenger.name, 'is assigned to Seat', seat_info.seat_no,
    'on the way to', departure_info.destination) AS Travellers_info
FROM passenger, seat_info, departure_info, seat_passenger, manages
WHERE passenger.Pass_id=seat_passenger.pass_id
  AND seat_passenger.Seat_id=seat_info.Seat_id
  AND seat_info.seat_id=manages.Seat_id
  AND manages.Dept_id=departure_info.dept_id

or using the || operator:

SELECT passenger.name || 'is assigned to Seat'
     || seat_info.seat_no || 'on the way to'
     || departure_info.destination AS Travellers_info
FROM passenger, seat_info, departure_info, seat_passenger, manages
WHERE passenger.Pass_id=seat_passenger.pass_id
  AND seat_passenger.Seat_id=seat_info.Seat_id
  AND seat_info.seat_id=manages.Seat_id
  AND manages.Dept_id=departure_info.dept_id
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Have you tried double-checking all your tables (especially the joining tables like "manages" and "seat_passenger") to make sure you have valid data in them that would appropriately join up?

Also, make sure you have spaces in your text.

e.g.: 'is assigned to Seat' should be ' is assigned to Seat '

Kcoder
  • 3,422
  • 4
  • 37
  • 56
1

If you are getting no data it must be in your join criteria. Reduce your select to just a column from your first table and then join each table one at a time and see when it is that you stop getting data back

Gratzy
  • 9,164
  • 4
  • 30
  • 45
0

First off, you're going to want to put a space before is so that it won't look mushed together.

    passenger.name + ' is assigned to seat'

Typically, you use subqueries and left outer joins for something of this sort. You do not just want to pull out all the data at once from all those tables. Let me see if I can explain this with some SQL. If you don't understand my example, go here: http://thenewboston.org/watch.php?cat=49&number=20 and watch 20-23 and you'll for sure understand it.

SELECT c.name + ' is assigned to Seat ' + c.seat_no + ' on the way to ' + departure_info.destination as Travellers_info 
FROM departure_info 
LEFT OUTER JOIN (SELECT b.Pass_id, b.name, b.seat_id, b.seat_no 
                 FROM seat_no 
                 LEFT OUTER JOIN (SELECT a.Pass_id, a.name, seat_passenger.seat_id 
                                  FROM seat_passenger 
                                  LEFT OUTER JOIN (SELECT passenger.name, passenger.Pass_id 
                                                   from passenger 
                                                   LEFT OUTER JOIN 
                                                     ON passenger.Pass_id = seat_passenger.Pass_id) a seat_info 
                                    ON a.seat_id = seat_info.seat_id) b manages 
                   ON b.seat_id = manages.seat_id) c departure_info 
  ON c.Dept_id = departure_info.Dept_id

I do not believe this will yield you the correct answer. But, it will get you working towards the right answer. I think I made a mistake after the third subquery. Remember that SQL evaluates things from the inside, then goes out. Just keep that in mind. Hope this helps.

Nikola Markovinović
  • 18,963
  • 5
  • 46
  • 51
Mardin Yadegar
  • 437
  • 4
  • 10
0

try this in MSSQL or MSAccess.

for query optimization use ON (Join)

SELECT passenger.name + 'is assigned to Seat '
 + ltrim(rtrim(convert(char,seat_info.seat_no))) + ' on the way to'
 + departure_info.destination AS Travellers_info
FROM passenger p
join    seat_passenger sp on sp.pass_id = p.Pass_id
join    seat_info s on s.Seat_id = sp.Seat_id
join    manages m on m.Seat_id = s.Seat_id
join    departure_info d on d.dept_id = m.dept_id

concat in 2008R2 is not a valid function so you need to convert a number data to character. while in 2012 it is available.

RoMEoMusTDiE
  • 4,739
  • 1
  • 17
  • 26