Solved

return all the messages with profile returned

Posted on 2013-11-03
8
325 Views
Last Modified: 2013-11-08
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
Comment
Question by:rgb192
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39620408
"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
 

Author Comment

by:rgb192
ID: 39620411
Error Code: 1054. Unknown column 'ck.sender' in 'on clause'
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39620420
? it works for me
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39620425
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39620430
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
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39620435
by the way "ck" is my abbreviation for check

you can choose your own aliases, just change the ck to something you prefer
0
 

Author Closing Comment

by:rgb192
ID: 39629606
lowercase ck works great.
Thanks
0
 

Author Comment

by:rgb192
ID: 39633286
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question