Ask Experts Questions for FREE Help !
Ask
    LTheobald's Avatar
    LTheobald Posts: 1,051, Reputation: 127
    Ultra Member
     
    #1

    Mar 15, 2006, 02:19 AM
    Select Help
    I haven't done much SQL for a few years so I wonder if somebody could give me a few pointers on something. Say I have the following to tables:

    Product
    ProductID (PK)
    Name

    Message
    MessageID (PK)
    MessageType
    ProductID (FK)

    Product's may be related to 0 or more messages.

    What I want is a select statement that will retrieve the product ID, name and then any messages all in one row. So for example:

    Code:
    ID      Name    MessageType
    ABC001  TestA   MessageA
    ABC002  TestB   MessageB, MessageC
    ABC003  TestC   MessageA, MessageC
    Is that possible?
    cajalat's Avatar
    cajalat Posts: 469, Reputation: 66
    Full Member
     
    #2

    Mar 15, 2006, 10:03 PM
    The closest I can come up with is this select statement:

    Code:
    select concat(Product.ProductID," ",Name," ",MessageType) from Product,Message
    where Product.ProductID=Message.ProductID;
    This is basically a join from two tables (Product and Message) and would produce something like this:


    Code:
    ID     Name  MessageType
    ABC001 TestA MessageA
    ABC002 TestB MessageB
    ABC002 TestB MessageC
    ABC003 TestC MessageA
    ABC003 TestC MessageC
    You could return the result of this select statement into an array and go through a loop where so long as the ID is the same as the previous to only print the MessageType. Once the ID changes then you can start a new line where you begin the printing with the ID, Name, and MessageType.

    Hope that helps.

    Casey
    LTheobald's Avatar
    LTheobald Posts: 1,051, Reputation: 127
    Ultra Member
     
    #3

    Mar 16, 2006, 01:58 AM
    Cheers Casey,

    That's also as far as I got. I'll just have to keep exporting it into Excel and filtering it then :) Thanks for the help.
    cajalat's Avatar
    cajalat Posts: 469, Reputation: 66
    Full Member
     
    #4

    Mar 16, 2006, 05:32 AM
    No no no... You can't let me get away that easily :)

    I think I figured it out... this was driving me nuts. Try this:


    Code:
    mysql> SELECT Product.ProductID,Name, GROUP_CONCAT(DISTINCT
    MessageType separator ',') AS 'Message Type' FROM Product,Message
      WHERE Product.ProductID=Message.ProductID GROUP BY Product.ProductID;
    +-----------+-------+-------------------+
    | ProductID | Name  | Message Type      |
    +-----------+-------+-------------------+
    |         1 | TestA | MessageA          |
    |         2 | TestB | MessageB,MessageC |
    |         3 | TestC | MessageA,MessageC |
    +-----------+-------+-------------------+
    3 rows in set (0.00 sec)
    
    mysql>
    I was beating my head against this as it should have worked. In the end it turned out that I needed a newer version of MySQL since the key was that the GROUP_CONCAT wasn't available until later releases of MySQL. It didn't work in MySQL 4.0 for me but did in 4.1.

    Good luck.

    Casey
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #5

    Mar 16, 2006, 07:50 AM
    Hmm, That Group_Concat function is neat, but is LT working in MY SQL or what? I do have VBA code for Access that will do this if that's the platform you are working on. Let me know.
    cajalat's Avatar
    cajalat Posts: 469, Reputation: 66
    Full Member
     
    #6

    Mar 16, 2006, 09:41 AM
    Good question Scott. I figured I'd make an assumption and go from there to narrow the problem down.

    I do believe that the GROUP_CONCAT() function is MySQL specific but from what I understand Oracle has a similar function called LIST() but I don't know how to use it.

    I'm thinking/hoping now that we can figure out how to do it in MySQL we can tailor it to other SQLs.

    Casey
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #7

    Mar 16, 2006, 10:54 AM
    I don't think Access SQL has any analogous function. This has to be done by looping through the recordset and concatenating the values.
    LTheobald's Avatar
    LTheobald Posts: 1,051, Reputation: 127
    Ultra Member
     
    #8

    Mar 17, 2006, 01:56 AM
    I'm using SQL Server but from a quick search I did come across something on MSDN saying the function was there. I just haven't had a chance to try it yet :)

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

State Select Water Heater [ 24 Answers ]

Power direct vent water heater blower will not shut off. Plenty of hot water. Gas water heater. 75 gallons. State select model pr6 75n rvt. The blower is integral to the unit. I could not find the exact model number on the web site and did not find the problem under the troubbl shooting guide....

Winxp pro-please select the operating system to start [ 2 Answers ]

I did what I thought was a clean install of xp pro --I think it has installed itself on both the C and D partitions --if so can this be easily fixed or will I have to re-install and re- activate with Microsoft again??

Buyers Agent.. How to select [ 1 Answers ]

How do you set about selecting a buyers agent to represent you if you are looking for a possible House purchase in different parts of a (USA) State? After all, an Agent can't be familiar with every area you might be looking at AT


View more questions Search