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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:07:04+00:00 2026-06-05T09:07:04+00:00

I have the query below which uses a union to pull data from three

  • 0

I have the query below which uses a union to pull data from three tables.

SELECT * FROM (SELECT 

    *,'table1' AS 'tbl', colour AS get_colour(created_date) FROM table1 WHERE customer = '11' UNION SELECT

    *,'table2' AS 'tbl', colour AS get_colour(created_date) FROM table2 WHERE customer = '11' UNION SELECT

    *,'table3' AS 'tbl', colour AS get_colour(created_date) FROM table5 WHERE customer = '11' 

    ORDER BY colour ASC

) as c

I am trying write the function below into SQL so that I can sort my results by colour.

My PHP example function is as follows.

function get_colour($date) {

    $days = floor((time() - strtotime($date))/(60*60*24));

    if(empty($date) || $date == "-") return "grey";

    elseif($days <= 7) return "green";

    elseif($days <= 14) return "orange";

    else return "red";
}

I have looked into using CASE SELECT and IF STATEMENTS but need a little help getting started.

Any help or direction would be appreciated.

  • 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-05T09:07:05+00:00Added an answer on June 5, 2026 at 9:07 am

    I won’t give you an out-of-the-box solution but consider this query:

    SELECT
        IF(DATEDIFF(TODAY, YESTERDAY) <= 7, "green",
            IF(DATEDIFF(TODAY, YESTERDAY) <= 14, "orange",
                "red")) as COLOUR
    FROM (
        SELECT
        NOW() as TODAY,
        DATE_SUB(NOW(), INTERVAL 15 DAY) as YESTERDAY) T;
    

    It should help you 😉

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

Sidebar

Related Questions

I have a query below which displays these results: SELECT q.QuestionId, q.QuestionContent, an.Answer FROM
I have a result from a query like the below, which does not have
I have this query (shown below) which currently uses temporary and filesort in order
I'm using Sql-Server, and I have the below query which returns all columns that
I have the query below (shorted up all the fields in the select to
I have a basic query below where it searches a question from the database
I have an SQL query (below) that essentially takes a student from tbStudents, and
I have the below query where I join two tables, the query is working
A query (see below) that extracts climate data from weather stations within a given
Let's say I have query structured like below which indicates dates search.asp?date=091210 I want

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.