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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:45:58+00:00 2026-05-17T02:45:58+00:00

Using PHP and MySQL I’m trying to get all the distinct values out the

  • 0

Using PHP and MySQL I’m trying to get all the distinct values out the number and type columns from all 4 of the tables listed below:

table_1

ID|t1_number|t1_type
1|1|new
2|1|old
3|2|new
4|3|new
5|1|old

table_2

ID|t2_number|t2_type
1|1|future
2|1|new
3|3|past
4|3|new
5|1|new

table_3

ID|t3_number|t3_type
1|1|past
2|1|new
3|1|new
4|1|new
5|1|old

table_4

ID|t4_number|t4_type
1|1|new
2|4|new
3|3|old
4|2|new
5|1|new

The values I want from the above tables would be:

numbers: 1,2,3,4

types: new,old,future,past

Here is what I have so far; but I’m not sure if the SQL is correct or how to format the while loop to get the values out.

$sql = "SELECT DISTINCT table_1.t1_number, table_2.t2_number, table_3.t3_number, table_4.t4_number, table_1.t1_type, table_2.t2_type, table_3.t3_type, table_4.t4_type
FROM table_1 
JOIN table_2
JOIN table_3
JOIN table_4";

$result = @mysql_query($sql, $con) or die(mysql_error());

while($row = mysql_fetch_array($result)) {

    $numbers= $row[?????????];

}
  • 1 1 Answer
  • 1 View
  • 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-17T02:45:59+00:00Added an answer on May 17, 2026 at 2:45 am

    Doing this in a single query risks duplicating a value in the output for either the number or type column, in the case that one list has more values than the other. To get a distinct list of values for either column, this needs to be separate queries:

    Number

    SELECT t1_number AS num
      FROM TABLE_1
    UNION
    SELECT t2_number
      FROM TABLE_2
    UNION
    SELECT t3_number
      FROM TABLE_3
    UNION
    SELECT t4_number
      FROM TABLE_4
    

    Type

    SELECT t1_type AS type
      FROM TABLE_1
    UNION
    SELECT t2_type
      FROM TABLE_2
    UNION
    SELECT t3_type
      FROM TABLE_3
    UNION
    SELECT t4_type
      FROM TABLE_4
    

    There’s no value to running DISTINCT in this UNION query, because duplicates will be removed, and this deals with only one column.

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

Sidebar

Related Questions

Using PHP/MySQL I'm trying to create a select statement that gets the data from
i am writing a site from scratch using php/mysql and am using GET requests
Using PHP / MySQL all encoded up as UTF, we have recently had to
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
Im trying to create a iPhone Objective C login page using PHP/MySQL to authenticate.
using php and mysql I have two tables, a users table and a profiles
Using PHP and MySQL, I want to select only 6 rows from table which
I am using a PHP mySQL query. Struggling with a Joins. mysql_query(SELECT * FROM
I am trying to make an sample web app using php mysql, where if
Using PHP / MySQL I have two tables one is customers the other is

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.