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 8271293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:48:06+00:00 2026-06-08T06:48:06+00:00

I am trying to write an SQL query that will sort based on a

  • 0

I am trying to write an SQL query that will sort based on a date (which is an INTEGER in the database) that could be in 1 of 2 tables, or both.

The fields in the tables this is in regards to are:

assignments:
– id
– id_lead
– date_assigned

leads:
– id
– date_added

the tables have other fields, but they do not apply to this question.

Now what i want, is if there is no assignments for the lead, I want the “date_sort” variable to be the date_added value of the lead. if there IS an assignment for the lead, i want it to be the date_assigned value of the assignment.

my problem is, using my query just doesn’t show any of the leads that do not have an assignment in my results list.

heres my query im working on:

$pagi_sql = "select 
             l.id, l.id_infusionsoft, l.name_first, l.name_last, l.postcode, a.website as a_website, l.website as l_website, l.date_added,
             a.id_dealership, a.id_lead, a.id as id_assign, a.date_assigned,
             d.name as dealership,
             if(a.id = null, l.date_added, a.date_assigned) as date_sort
             from `leads` as l
             left join `assignments` as a on (a.id_lead = l.id)
             left join `dealerships` as d on (d.id = a.id_dealership)
             $where
             order by date_sort desc";

heres my original query, which works, but is not sorting the way i want it to:

$pagi_sql = "select 
             l.id, l.id_infusionsoft, l.name_first, l.name_last, l.postcode, a.website as a_website, l.website as l_website, l.date_added,
             a.id_dealership, a.id_lead, a.id as id_assign, a.date_assigned,
             d.name as dealership
             from `leads` as l
             left join `assignments` as a on (a.id_lead = l.id)
             left join `dealerships` as d on (d.id = a.id_dealership)
             $where
             order by l.date_added desc";

leads can have multiple assignments….i think this is the problem…but i cant figure out how to get it to sort the way i want.

should i change these up, so that im selecting from the assignments table instead? but then i run into the problem of, how do i show the unassigned leads in the same table sorted by the date_added/date_assigned?

any help would be awesome.

  • 1 1 Answer
  • 0 Views
  • 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-06-08T06:48:07+00:00Added an answer on June 8, 2026 at 6:48 am

    Change this line:

    if(a.id = null, l.date_added, a.date_assigned) AS date_sort
    

    To this:

    if(a.id IS NULL, l.date_added, a.date_assigned) AS date_sort
    

    The reason why the first line isn’t sorting the way you want it to is because the false condition of IF is always being executed since NULL is an unknown value and cannot equal to anything, not even itself.

    You must use IS NULL instead of = NULL.

    Read my answer to this question for more insight on NULL values.


    Edit: If you want to be more succinct, you can do:

    COALESCE(a.date_assigned, l.date_added)
    

    Th COALESCE() function will return the first non-NULL argument.

    If you have no need to display the value of date_sort, you can even put it straight into the ORDER BY clause:

    ORDER BY COALESCE(a.date_assigned, l.date_added)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a custom SQL query that will create a list of
I'm trying to write a query for a database that will left join a
Im trying to write an SQL query that will check in the table ‘persons’
I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that
I'm trying to write an SQL query that will gather certain data in one
I'm trying to write an SQL query that would search within a CSV (or
I have a SQL query that I'm trying to write, but I'm not quite
So I'm trying to write a SQL Server Trigger that will call an ASP.Net
I'm trying to write a linq-to-sql query using || that behaves the same way
I'm having trouble with my sqlite database. I'm trying to write an query that

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.