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

  • Home
  • SEARCH
  • 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 661325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:15:17+00:00 2026-05-13T23:15:17+00:00

I need to take rows from two separate tables, and arrange them in descending

  • 0

I need to take rows from two separate tables, and arrange them in descending order by their date. The rows do not correspond in order or number and have no relationship with each other.

They each contain updates on a site, one table holds text, links, dates, titles etc. from a blog. The other has titles, links, specifications, etc. from images. I want to arrange some basic information (title, date, small description) in an updates section on the main page of the site, and for it to be in order of date.

Merging them into one table and modifying it to suit both types isn’t what I’d like to do here, the blog table is WordPress‘ standard wp_posts, and I don’t feel comfortable adding columns to make it suit the image table too. I’m afraid it could clash with upgrading later on and it seems like a clumsy solution (but that doesn’t mean I’ll object if people here advise me it’s the best solution).

Here are the DESCRIBES of each table:

mysql> describe images;
+---------+--------------+------+-----+-------------------+----------------+
| Field   | Type         | Null | Key | Default           | Extra          |
+---------+--------------+------+-----+-------------------+----------------+
| id      | int(11)      | NO   | PRI | NULL              | auto_increment |
| project | varchar(255) | NO   |     | NULL              |                |
| title   | varchar(255) | NO   |     | NULL              |                |
| time    | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
| img_url | varchar(255) | NO   |     | NULL              |                |
| alt_txt | varchar(255) | YES  |     | NULL              |                |
| text    | text         | YES  |     | NULL              |                |
| text_id | int(11)      | YES  |     | NULL              |                |
+---------+--------------+------+-----+-------------------+----------------+

mysql> DESCRIBE wp_posts;
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field                 | Type                | Null | Key | Default             | Extra          |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID                    | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| post_author           | bigint(20) unsigned | NO   |     | 0                   |                |
| post_date             | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_date_gmt         | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_content          | longtext            | NO   |     | NULL                |                |
| post_title            | text                | NO   |     | NULL                |                |
| post_excerpt          | text                | NO   |     | NULL                |                |
| post_status           | varchar(20)         | NO   |     | publish             |                |
| comment_status        | varchar(20)         | NO   |     | open                |                |
| ping_status           | varchar(20)         | NO   |     | open                |                |
| post_password         | varchar(20)         | NO   |     |                     |                |
| post_name             | varchar(200)        | NO   | MUL |                     |                |
| to_ping               | text                | NO   |     | NULL                |                |
| pinged                | text                | NO   |     | NULL                |                |
| post_modified         | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_modified_gmt     | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_content_filtered | text                | NO   |     | NULL                |                |
| post_parent           | bigint(20) unsigned | NO   | MUL | 0                   |                |
| guid                  | varchar(255)        | NO   |     |                     |                |
| menu_order            | int(11)             | NO   |     | 0                   |                |
| post_type             | varchar(20)         | NO   | MUL | post                |                |
| post_mime_type        | varchar(100)        | NO   |     |                     |                |
| comment_count         | bigint(20)          | NO   |     | 0                   |                |
+-----------------------+---------------------+------+-----+---------------------+----------------+

I can do this easily with a single table like this (I include it here in case I’m using an over-elaborate method without knowing it):

$content = mysql_query("SELECT post_title, post_text, post_date FROM posts ORDER BY post_date DESC");
while($row = mysql_fetch_array($content))
{
    echo $row['post_date'], $row['post_title'], $row['post_text'];
}

But how is it possible to call both tables into the same array to arrange them correctly?

By correctly, I mean that they will intermix their echoed results based on their date. Maybe I’m looking at this from the wrong perspective, and calling them to a single array isn’t the answer?

Additionally, I need a way to form a conditional expression based on which table they came from, so that rows from table 1 get echoed differently than rows from table 2?

I want results from table 1 to be echoed differently (with different strings concatenated around them, I mean) for the purpose of styling them differently than those from table two. And vice versa.

I know an if...else statement would work here, but I have no idea how can I write the expression that would determine which table the row is from.

  • 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-13T23:15:17+00:00Added an answer on May 13, 2026 at 11:15 pm

    The PHP-based sorting options aren’t always the best idea because you might want to paginate your results, at which point you run into difficulties.

    The UNION-based query described above will work better, but you will need to tweak it to allow for the different schemas of the tables:

    (
      Select 'Image' as data_type, id, title, time as date_posted, ....
      From images
    )
    UNION
    (
      Select 'Post', ID as id, post_title as title, post_date as date_posted, ...
      From wd_posts
    )
    Order By date_posted Desc Limit $foo, $bar
    

    The key is that you should pull the same number of columns from both tables. The column names don’t have to be the same to make it work, but I’ve made them the same above.

    I also think you should put indexes on images.time and wd_posts.post_date.

    Mixed-format ordering by the time (timestamp) and post_date (datetime) fields will work on MySQL 5, I just tried it.

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

Sidebar

Related Questions

I want to take some data from the database and arrange it into rows
i need to take some row. They came from sql TARIH (sql column) is
I need to transfer a large number of rows from a SQL Server database
I have a situation where I need to take a quantity consumed from one
I need to take the first N rows for each group, ordered by custom
I have two large tables -Master table A: 1.4million rows -Detail table B: 9
I need to match two very large Numpy arrays (one is 20000 rows, another
I need to take random row from table. At first, i wrote SP like
I have two tables, which I need to join by one column and is
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from

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.