Solved

return all the messages with profile returned

Posted on 2013-11-03
8
308 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now