Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

return all the messages with profile returned

http://www.experts-exchange.com/Database/MySQL/Q_28283780.html#a39619770
Currently this query returns one row of sender, message_id

| SENDER | MESSAGE_ID |
|--------|------------|
|    abl | 3609276896 |


I would like to see all the rows of messages with profile_id 'abl'

Because when I check results, I have to type select * from a_messages where profile_id='abl'
0
rgb192
Asked:
rgb192
  • 5
  • 3
1 Solution
 
PortletPaulCommented:
"nest" the previous query as a subquery, join back to messages via sender (so all sender messages get included), order the results as you like
SELECT
*
FROM (
      SELECT
              m.sender
            , m.message_id
      FROM (
            SELECT
              profile_id
            , min(message_id) AS message_id
            FROM a_messages2
            GROUP BY
              profile_id
           ) AS mn
      INNER JOIN a_messages2 AS m ON mn.message_id =  m.message_id
      WHERE m.profile_id = m.sender
     ) AS CK
INNER JOIN a_messages2 AS m2 ON ck.sender =  m2.sender 
ORDER BY
        m2.profile_id
      , m2.message_id ASC 
;

Open in new window

0
 
rgb192Author Commented:
Error Code: 1054. Unknown column 'ck.sender' in 'on clause'
0
 
PortletPaulCommented:
? it works for me
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
PortletPaulCommented:
unchanged query with result displayed
**Query 1**:

    SELECT
    *
    FROM (
          SELECT
                  m.sender
                , m.message_id
          FROM (
                SELECT
                  profile_id
                , min(message_id) AS message_id
                FROM a_messages2
                GROUP BY
                  profile_id
               ) AS mn
          INNER JOIN a_messages2 AS m ON mn.message_id =  m.message_id
          WHERE m.profile_id = m.sender
         ) AS CK
    INNER JOIN a_messages2 AS m2 ON ck.sender =  m2.sender 
    ORDER BY
            m2.profile_id
          , m2.message_id ASC 
    

**[Results][2]**:
    
    | SENDER | MESSAGE_ID | A_MESSAGES_ID | PROFILE_ID | THIS_USER |
    |--------|------------|---------------|------------|-----------|
    |    abl | 3609276896 |            13 |        abl |    staff3 |
    |    abl | 3609276896 |            12 |        abl |    staff3 |
    |    abl | 3609276896 |            10 |        abl |    staff3 |
    |    abl | 3609276896 |             7 |        abl |    staff3 |
    |    abl | 3609276896 |             2 |        abl |    staff3 |
    |    abl | 3609276896 |            16 |        abl |    staff3 |
    |    abl | 3609276896 |            23 |        abl |    staff3 |
    |    abl | 3609276896 |            20 |        abl |    staff3 |
    |    abl | 3609276896 |            25 |        abl |    staff3 |
    |    abl | 3609276896 |            28 |        abl |    staff3 |
    |    abl | 3609276896 |            30 |        abl |    staff3 |
    |    abl | 3609276896 |            32 |        abl |    staff3 |

Open in new window

see: http://sqlfiddle.com/#!9/8efe9/4
0
 
PortletPaulCommented:
the "alias" CK is defined on line 19

it is then referenced in the next line "ON ck.sender..." see the ck?

perhaps try making the "alias" in lower case, so change line 19 to this:

         ) AS ck
0
 
PortletPaulCommented:
by the way "ck" is my abbreviation for check

you can choose your own aliases, just change the ck to something you prefer
0
 
rgb192Author Commented:
lowercase ck works great.
Thanks
0
 
rgb192Author Commented:
Thanks for your query.

I have a related query where I would want to see all the messages (the messages I sent and received-- not just received)


http://www.experts-exchange.com/Database/MySQL/Q_28288994.html
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now