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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:17:37+00:00 2026-06-17T21:17:37+00:00

I have a database with articles and stock information (two tables). When entering the

  • 0

I have a database with articles and stock information (two tables). When entering the article id, I select the article from the article table, do some other stuff and then look in the stock table if the article is available.

This could be an easy peasy SELECT * FROM articles, BUT: my articles table has a field parent_id. If that is set, I should select the parent row. Let me illustrate:

+----+-----------+---------+---------+
| id | parent_id | article | country |
|----|-----------|---------|---------+
|  1 |           | abc     | uk      |
|  2 |           | cde     | us      |
|  3 |           | efg     | can     |
|  4 | 6         | hij     | mex     |
|  5 |           | klm     | bra     |
|  6 |           | nop     | us      |
|  7 | 8         | qrs     | mex     |
|  8 |           | tuv     | uk      |
+----+-----------+---------+---------+

If I search for article abc, it should return row 1 with country UK. If I’m searching for hij, the query should return row 6, with country US instead of row 4, with country mex. Is there any way of doing this in MySQL or is it faster/better/the only way to split it up in two queries with PHP?

  • 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-17T21:17:38+00:00Added an answer on June 17, 2026 at 9:17 pm

    You can do it with a LEFT outer join:

    SELECT 
        COALESCE(p.id, a.id) AS id
      , COALESCE(p.country, a.country) AS country 
    FROM 
            articles AS a
        LEFT JOIN 
            articles AS p
                ON p.id = a.parent_id 
    WHERE
        a.article LIKE '%hij%' ;          -- or:  a.article = 'hij'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database articles With the following fields id author
I currently have a database with two tables called Articles and Tags . In
I have a database where you can select articles etc, users have an account,
I have a MySQL database with 3 tables: articles, tags, and articles_tags The schema
I have a MySQL database with 3 tables: **articles:** id - articletitle - articleorganization
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
I have a MySQL database of newspaper articles. There's a volume table, an issue
I have a database table with articles and each one of this articles have
I have a Database that contains data about articles , structures and manufacturers .
I have a MYSQL database with 50.000 rows. Each row represents an article. I

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.