简体   繁体   中英

How do I query a user value in one table to a column header and get value under header?

Situation

I have two tables:

表

  • SGL= Single (one person in room)
  • DBL = Double (two people in the room)
  • TPL = Triple (three people).

and so on. (CHD = child in room; discounted occupancy).

The clients table contains user inserted values. The tourprices table is preset rates for roomtypes.

Question

Is there a way to query such that the user inserted roomtype is compared to the column header of tourprices table such that the result is the price for the date under that column header?

For example: the user wishes to book two people (DBL) onto the tour for 27-Feb-2012. How do I write the query to pull the $200 rate based on the given?

SELECT c.roomtype, t.SGL, t.DBL, t.TPL, t.QUD, t.CHD
FROM clients c, tourprices t
WHERE c.roomtype = ??????

thanks!

You could use a CASE Statement to look at what value Roomtype is and grab the appropriate column from Tourprices that has that type to determine the price.

SELECT c.Roomtype
   , CASE
        WHEN c.Roomtype = 'SGL' THEN tp.SGL
        WHEN c.Roomtype = 'DBL' THEN tp.DBL
        WHEN c.Roomtype = 'TPL' THEN tp.TPL
        WHEN c.Roomtype = 'QUD' THEN tp.QUD
        WHEN c.Roomtype = 'CHD' THEN tp.CHD
     END AS RateOfTour
FROM Clients AS c
INNER JOIN Tourprices AS tp ON c.tourstart = tp.tourstartp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM