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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:53:18+00:00 2026-06-07T04:53:18+00:00

I have two tables. First table looks like this: Table: Records rid | user

  • 0

I have two tables. First table looks like this:

Table: Records

    rid  |   user id   |     title     |    whenadded    | public
--------------------------------------------------------------------
    1        212           Example        2012-06-28         1
    2        217           Test Rec       2012-07-05         1
    3        212           Another        2012-07-02         1
    4        212           Unlisted       2012-05-02         0
    5        217           Success        2012-04-08         1
    6        238           Always         2012-04-18         1

Table: Likes

    id  |   user id   |     rid     |    whenliked
------------------------------------------------------
    1        212            2            2012-07-06
    2        205            1            2012-06-30
    3        212            5            2012-07-04

In the ‘Records’ table, ‘rid’ is set as the primary index. In the ‘Likes’ table, id is set as the primary index.

I’m using PHP. PHP will provide a value to MySQL to use as reference. I’d like to have a single MySQL query that will do the following:

Pseudo Code:

$userid = 212;
$SQL = 'SELECT DISTINCT records.* 
        FROM records,likes 
        WHERE (records.userid = ' . $userid . 
          ' AND records.public = 1) 
            OR (records.id = likes.rid AND likes.userid = ' . $userid . 
          ' AND records.public = 1) 
        ORDER BY likes.whenliked DESC, records.whenadded DESC 
        LIMIT 50;';

Look at the $SQL query I just provided above. That’s my attempt at developing the query I wanted, but it didn’t achieve what I was looking for; it came pretty damn close, but it was still ordering incorrectly. The query was developed as best as I could first by myself, then based on what I found by searching for a solution on StackFlow and elsewhere on Google.

Those are the conditions I’m trying to order it by:

  1. All records selected must be public (records.public = 1).
  2. If the record belongs to the user (in this example, 212), then order the records by records.whenadded.
  3. If the record doesn’t belong to the user, but it’s a record the user liked, then order the records by likes.whenliked.
  4. Dates would be ordered from newest to oldest.

The end result from the query returned would look like this (keep in mind, whenliked is NOT in the return data, it’s just there for reference so you see how it’s ordered):

    rid  |   user id   |     title     |    whenadded    | public   |  whenliked {not incl}
------------------------------------------------------------------------------------
    2        217           Test Rec       2012-07-05         1        2012-07-06
    3        212           Another        2012-07-02         1
    5        217           Success        2012-04-08         1        2012-06-30
    1        212           Example        2012-06-28         1

Hope that makes sense. Feel free to ask questions, I’ll clarify the best I can.

Thanks in advance for your time, your consideration, and for reading this. Even if there is no response or no solution is found, your time is still very much 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-07T04:53:19+00:00Added an answer on June 7, 2026 at 4:53 am

    The field you are ordering by doesn’t have to be an existing field in the database. You can also use a field you define in your select:

    SELECT IF(records.userid = ' . $userid . ', records.whenadded, likes.whenliked) as date 
    

    Now you can use it in the order by part:

    ORDER BY date DESC
    

    From the MySQL Manual:

    A select_expr can be given an alias using AS alias_name. The alias is used as the expression’s column name and can be used in GROUP BY, ORDER BY, or HAVING clauses

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

Sidebar

Related Questions

I have two tables. The first table is Requests and it looks like this
I have two tables which looks as below First table : DealNum CurrencyValue CurrencyCode
I have two tables. The first table has a column with a lot of
I have a database framework where I have two tables. The first table has
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
I have two table views, when user clicks one cell of the first, second
This is for an upcoming project. I have two tables - first one keeps
I have SQL Server 2008 database with two tables. The first table is called
I have two tables which I need to join. First Table: EFF_DATE RATE CURRENCY
I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to

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.