Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:21:11+00:00 2026-05-17T01:21:11+00:00

I want to do this query: SELECT * FROM mail WHERE tag_draft = 0

  • 0

I want to do this query:

  SELECT * 
    FROM mail 
   WHERE tag_draft = 0 
     AND (target_id = 2 OR source_id = 2) 
GROUP BY created DESC`

…but it returns no rows. Now, if I do this query:

  SELECT * 
    FROM mail 
   WHERE tag_draft = 0 
     AND target_id = 2 
GROUP BY created DESC

…then it works fine – every row with a target_id of 2 is selected. If I substitute target_id with source_id, it works fine too. The thing is, I want to select rows where the target OR source ID is 2 (2 used as an example here), however running the query first stated

(SELECT * FROM mail WHERE tag_draft=0 AND (target_id=2 OR source_id=2) GROUP BY created DESC)

, with or without inner brackets, returns no rows. I cannot figure out why this will not work, other than I’m doing something wrong with the OR bit.

Some example data:

source_id target_id etc_fields
-------------------------------
2         12        blah
12        2         blah
2         14        blah
2         10        blah
2         2         blah

All the above rows should be displayed in the table. what should NOT be displayed is stuff like:

source_id target_id etc_fields
-------------------------------
10        8         ...
255       16        ...
4         12        ...
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:21:12+00:00Added an answer on May 17, 2026 at 1:21 am

    Here’s one way, assuming there’s there’s not more than one row with the same MAX(created) for the given condition:

    SELECT m.* FROM mail m WHERE m.tag_draft=0 AND (m.target_id=2 OR m.source_id=2) AND
      m.created = (SELECT MAX(created) FROM mail sm 
           WHERE sm.tag_draft=0 AND (sm.target_id=2 OR smsource_id=2))
    

    Or perhaps just

     SELECT m.id,m.target_id,m,m.source_id,MAX(created) FROM mail m 
        WHERE m.tag_draft=0 AND (m.target_id=2 OR m,source_id=2) 
      GROUP BY m.id,m.target_id,m.source_id
    

    Make sure you GROUP BY every column that you select, except the aggregate ( MAX(created) ) , SELECT * won’t do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
Hi I want to execute this query SELECT * FROM nm_ticket WHERE id_urgencia IN
Say i have a query select username from users. I want to output this
I have a query select * from myTable ...and I want to wrap this
I want to create this query: select * from products where number in ('123',
This query: select * from op.tag where tag = 'fussball' Returns a result which
I am using this query: SELECT * from likes GROUP BY url ORDER BY
Let's say I want to perform this query: (SELECT a FROM t1 WHERE a=10
I want to simulate this query: SELECT * FROM FOO WHERE ID IN (1,2,3)
I want to optimize this query **SELECT * FROM Users WHERE Active = 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.