If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-03, 07:44
coolblu coolblu is offline
Registered User
 
Join Date: Dec 2003
Posts: 1
Query Problem

Hello All.

I am having a bit of trouble with a query I am making for a web site that references a music collection.

I have the following table structure:

table records
--- id (unique record id)
--- artistname (name of the recording artist(s))
--- title (title of the record)
--- price (price of the record)
--- rcon (The condition of the record an integer that is linked to the condition table)
--- scon (The condition of the sleeve an integer that is linked to the condition table)
--- description (a short description of the record)
--- label (the label the record was recorded on an integer that is linked to the labels table)

table labels
--- labelid (unique identifier of the label)
--- labelname (The actual name of the label)
--- labelnotes (Any notes regarding the recording label)

table condition
--- conditionid (Unique identifer for a condition in the conditions table)
--- abbr (An abbreviation of the condition's fullname e.g. "M" for Mint condition)
--- fullname (The fullname of the condition e.g."Mint")
--- condesc (A short description of the condition e.g. whatthe product is like for it to be categorised under that condition)


I have created the following query to get the label name and the record condition (rcon) and the (scon):

SELECT *
FROM records
LEFT JOIN labels AS labels ON ( records.label = labels.labelid )
LEFT JOIN condition AS recordcondition ON ( records.rcon = recordcondition.conditionid )
LEFT JOIN condition AS sleevecondition ON ( records.scon = sleevecondition.conditionid )
ORDER BY artistname ASC

However the problem with this query is that as the rcon and scon both reference the condition table (conditionid field) there are two values for "conditionid" (As each record has a separate rating for record condition and the condition of the sleeve).

My real question is that as the record and sleeve condition both come out in the query as "conditionid" is there anyway of renaming these in the query so that they can both be grabbed in a while loop in PHP without having to have separate columns in the condition table for record condition and sleeve condition??

I would ultimately like to be able to grab the corresponding "abbr" from the "conditionid" for both "rcon" and "scon". Can this be done in one query or do I have to have separate queries?

Any help you can give you be appreciated.

Thanks

'blu
Reply With Quote
  #2 (permalink)  
Old 12-17-03, 08:58
Mincer Mincer is offline
Registered User
 
Join Date: Sep 2003
Location: London
Posts: 56
When doing multi-table joins like that, it's usually advisable to explicitly list the columns you want to retrieve.

Code:
SELECT
    records.title AS title
  , records.artist AS artist
  , recordcondition.abbr AS rconabbr
FROM
    ....
Matt.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On