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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:35:21+00:00 2026-05-19T13:35:21+00:00

A rather complicated sql query that I might be making much more difficult that

  • 0

A rather complicated sql query that I might be making much more difficult that it should be:
I have two tables:

News:
newsid, datetime, newstext

Picture:
pictureid, datetime, imgPath

The two are not related, I am only joining by the date that the news/picture was created on

SQL so far:

SELECT * FROM news as n LEFT OUTER JOIN (SELECT count(pictureid), datetime 
FROM picture GROUP BY DATE(datetime)) as p ON DATE(n.datetime) = DATE(p.datetime) 
UNION 
SELECT * FROM news as n RIGHT OUTER JOIN (SELECT count(pictureid), 
datetime FROM picture GROUP BY DATE(datetime)) as p ON 
DATE(n.datetime) = DATE(p.datetime) 

I have to use union to simulate a full outer join in MySQL.
The results:

newsid     text     datetime  count()   datetime 
1       sometext   2011-01-16   1       2011-01-16 
2         moo2    2011-01-19  NULL        NULL 
3        mooo3    2011-01-19  NULL        NULL 
NULL      NULL      NULL       4         2011-01-14 

The problem being that I obviously end up with two date columns- one from news and one from pictures, this means i cannot order by date and have it be in the correct order! Any ideas? Even if it means restructuring the database! I need date to be in a single column.

The answer came from SeRPRo
The completed working code is:

SELECT `newsid`, `text`,
    CASE 
    WHEN `datetime` IS NULL 
    THEN `pdate` 
    ELSE `datetime` 
    END 
    as `datetime`, 

`pcount` FROM 
(
    (SELECT * FROM news as n LEFT OUTER JOIN 
        (SELECT count(pictureid) as pcount, datetime as pdate FROM picture GROUP BY DATE(datetime)) as p 
        ON DATE(n.datetime) = DATE(p.pdate) ORDER BY datetime
    )
    UNION
    (SELECT * FROM news as n RIGHT OUTER JOIN 
        (SELECT count(pictureid) as pcount, datetime as pdate FROM picture GROUP BY DATE(datetime)) as p 
        ON DATE(n.datetime) = DATE(p.pdate) ORDER BY datetime
    ) 

) as x
ORDER BY datetime
  • 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-05-19T13:35:22+00:00Added an answer on May 19, 2026 at 1:35 pm

    just using your database structure and your query, and since FULL OUTER JOIN is not available in MySQL, I think a solution could be this:

    SELECT
        `newsid`,
        `text`,
        CASE
            WHEN `datetime` IS NULL THEN `pdate`
            ELSE `datetime`
        END as `datetime,
        `pcount`
    (
    SELECT *
    FROM `news` as `n`
        LEFT OUTER JOIN (
                            SELECT count(pictureid) as `pcount`, datetime as `pdate`
                            FROM picture GROUP BY DATE(datetime)
                        ) as p ON DATE(n.datetime) = DATE(p.datetime) 
        UNION
    SELECT * 
        FROM `news` as `n`
        RIGHT OUTER JOIN (
                            SELECT count(pictureid) as `pcount`, datetime as `pdate`
                            FROM picture GROUP BY DATE(datetime)
                        ) as p ON DATE(n.datetime) = DATE(p.datetime) 
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather complicated query on my PostgreSQL database spanning 4 tables via
I have a rather complicated deploy setup for our Drupal site that is a
I have a (rather complicated) SQL statement where I select data from lots of
A rather complicated SQL query I was working on got me thinking about a
I have a rather complicated scenario that I have never really had to deal
I have a rather complicated setup with two columns full of elements. Clicking on
I have a PHP script that I am using to generate some rather complicated
I have a Linq-to-Entities query that is not complicated but requires an .include and/or
I have a rather complicated issue that I am trying to solve in PHP
After much work I finally got a rather complicated query to work very smootly

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.