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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:55:35+00:00 2026-06-16T20:55:35+00:00

I have been working with a postgresql database with a php project, and I

  • 0

I have been working with a postgresql database with a php project, and I am just trying to construct a query. This is the scenario.

Suppose I have several tables, FOO and BAR

Table FOO looks like this:

| foo_id | foo_name | foo_data  |
----------------------------------
|   1    | john     | son       |
|   2    | jane     | daughter  |
|   3    | sam      | son       |
|   4    | sally    | daughter  |

Table BAR looks like this

| bar_id | bar_foo_id | bar_fooParent_id | bar_content            |
----------------------------------
|   1    | 1          |       1          | yabba-dabba-doo        |
|   2    | 2          |       1          | scooby-scooby-doo      |
|   1    | 3          |       888        | don't have a cow, man  |
|   2    | 4          |       999        | d'oh!                  |

I can’t change the table schema, everything is as it is.

But – I can pass a value through PHP, let’s call it PARENT, that represents a value from bar_fooParent_id (so I can pass 1,2,3…657,888,999 etc).

And – foo_id in table FOO maps to bar_foo_id in table BAR.

I would like to build a SELECT Query that combines data from both tables FOO and BAR. Something like:

SELECT BAR.bar_content, (and other BAR.columns) , FOO.foo_name, FOO.foo_data 
FROM FOO,BAR WHERE bar_fooParent_id=".$PARENT." AND " ...?

where ? is a little confusing.

I need to grab rows for foo_name and foo_data from table FOO based upon the selected rows from table BAR. So if a value of 1 is passed to $PARENT, then bar_fooParent_id would be 1, I would get the first two rows from table BAR, and use their respective bar_foo_ids (with values 1 and 2) to grab the data from the rows of table FOO that have foo_ids of 1 and 2 (the first two rows in this case).

I have tried statements similar to those below (values are hard coded for simplicity)

SELECT * from BAR,FOO where BAR.bar_fooParent_id=1 AND (BAR.foo_id=1 OR BAR.foo_id=2) AND (FOO.foo_id=1 OR FOO.foo_id=3)

OR

select * from BAR where BAR.barr_fooParent_id=1 IN (SELECT foo_id,foo_name from kid WHERE foo_id=1 OR foo_id=3 ) 

without much success. Basically the data should return ideally as

foo_name  |  foo_data  | bar_content    | other BAR columns ... |
_________________________________________________________________
john      |  son       | yabba-dabba-do | etc.                  |
jane      |  daughter  | scooby-dooby-do| etc.                  |

(apologies for the formatting, not sure what is happening with this third table of results)

I’d appreciate it if someone can lend a hand in building this SELECT query for postgreSQL.

any ideas? and thanks.

Edward

  • 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-16T20:55:36+00:00Added an answer on June 16, 2026 at 8:55 pm

    Sounds like all you need is a simple join:

    SELECT f.foo_name, f.foo_data, b.bar_content
    FROM foo f
    INNER JOIN bar b ON b.bar_foo_id = f.foo_id
    WHERE b.bar_fooParent_id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this app for at least 3-4 months and just
I have been working to upgrade a Postgresql database from 8.4 to 9.1. I
We have been working on a project for several months without any problems until
Have been working on this question for a couple hours and have come close
I have been working on a project in C# (.net4). Project pretty much allows
I have been working on a project on and off, but I haven't touched
I have been working on a huge ETL project with 150+ tables and during
I have been working on this sort of ATM (With a maximum of 50
I've been tasked to create a PHP app which accesses an existing PostgreSQL database.
We have this PHP application which selects a row from the database, works on

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.