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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:41:26+00:00 2026-06-13T07:41:26+00:00

Is this possible? If so, how do I structure my SELECT statement to achieve

  • 0

Is this possible? If so, how do I structure my SELECT statement to achieve these results?

I am creating an HTML table in a PHP file that gets various rows from a MySQL database. This is my query:

     // Define the query:
     $query = "SELECT b.birdID, b.nameGeneral, b.nameSpecific, 
               b.genus, b.species, b.family, b.populationTrend, f.food, h.habitat 
               FROM bird_habitat AS bh 
               JOIN bird_food AS bf ON (bf.birdID_FK = bh.birdID_FK) 
               JOIN food AS f ON (f.foodID = bf.foodID_FK) 
               JOIN habitat AS h ON (h.habitatID = bh.habitatID_FK) 
               JOIN birds AS b ON (b.birdID = bh.birdID_FK AND b.birdID = bf.birdID_FK) 
               ORDER BY b.birdID ASC";

The problem is with the bird_habitat and bird_food tables. Each bird can have 2 different foods that it can eat and can also live in 2 different habitats. For example, a blackbird can eat seeds and insects and live in marshes and fields. When I run the SELECT statement, I get 4 results for each bird where I only want to get 2 results. Using the blackbird example, I get these rows returned:

**Bird**  **Food**       **Habitat**
    1       Seeds          Marsh
    1       Insects        Marsh
    1       Seeds          Field
    1       Insects        Field

What I want to get returned is this:

**Bird #**  **Food**       **Habitat**
    1         Seeds          Marsh
    1         Insects        Field

So basically, I want to return the unique foods that each bird can eat and the unique habitat they can live in without the redundant fields being repeated. Is this possible? If so, how do I structure my SELECT statement to achieve these results?

Here is a link to the page that the HTML table is displayed if you want to look at what is returned from the SELECT query above so that you can get a better idea of what I am trying to do:

http://www.jasonkmccoy.com/AB-Tech/web250/Mod07/McCoy_edit_delete/index.php

As you will see if you visit the page, where there are birds that have 2 foods and 2 habitats, I need to remove 2 rows so that only unique fields are returned.

  • 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-13T07:41:27+00:00Added an answer on June 13, 2026 at 7:41 am
    "SELECT b.birdID, b.nameGeneral, b.nameSpecific, 
    b.genus, b.species, b.family, b.populationTrend, f.food, h.habitat 
    FROM birds  
    JOIN bird_food AS bf ON (bf.birdID_FK = bh.birdID_FK) 
    JOIN food AS f ON (f.foodID = bf.foodID_FK) 
    JOIN habitat AS h ON (h.habitatID = bh.habitatID_FK) 
    JOIN bird_habitat AS bh  ON (b.birdID = bh.birdID_FK ) 
    group by f.foodID
    ORDER BY b.birdID ASC";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table (in MySQL) that stores votes. The structure is like this:
Is this possible to change bundle structure in Symfony 2, for example: MyBundle EntityName
Is this possible in .net to see visually ,thread structure those are running (in
Is this possible in any way? My project folder structure looks like this: my_project:
I have a table Action with a (part of the) structure like this: Action
I have a database table that has a structure like the one shown below:
Is this possible in TSQL? I am feeding this structure in via an xml
I'm creating a sproc that will insert rows into a 'staging' table with an
This is the table structure: for_id from_id for_folder from_folder deleted The number 2 represents
Table structure: country season points Current query: SELECT SUM(points) AS total, country FROM table

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.