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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:53:48+00:00 2026-06-10T12:53:48+00:00

Need some help with a query. I have 2 tables in a database, contacts

  • 0

Need some help with a query. I have 2 tables in a database, contacts & tasks.

mysql> describe contacts;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| contact_id | int(11)      | NO   | PRI | NULL    | auto_increment |
| last_name  | varchar(100) | YES  |     | NULL    |                |
| first_name | varchar(100) | YES  |     | NULL    |                |
| email      | varchar(50)  | YES  |     | NULL    |                |
| phone      | varchar(20)  | YES  |     | NULL    |                |
| school_id  | varchar(12)  | NO   |     | NULL    |                |
| access     | char(1)      | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

mysql> describe tasks;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| task_id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| task_name         | varchar(125) | YES  |     | NULL    |                |
| task_date         | date         | YES  |     | NULL    |                |
| task_description  | text         | YES  |     | NULL    |                |
| contact_id1       | int(11)      | YES  |     | NULL    |                |
| contact_id2       | int(11)      | YES  |     | NULL    |                |
| contact_id3       | int(11)      | YES  |     | NULL    |                |
| contact_id4       | int(11)      | YES  |     | NULL    |                |
| contact_id5       | int(11)      | YES  |     | NULL    |                |
| contact_id6       | int(11)      | YES  |     | NULL    |                |
| completed         | char(1)      | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

I want to get the info from the tasks table but I want to replace the contact_id1-contact_id6 with some of the fields from the matching contact_id. I have been messing around with nested select statements which will work but is very messy. It just seems there has to be a cleaner way to do this. I thought I had some with this,

SELECT tasks.task_id, tasks.task_name, tasks.reminder_time, tasks.reminder_interval, CONCAT (contact_1.first_name, " ", contact_1.last_name) as contact_1_name, CONCAT(contact_2.first_name, contact_2.last_name) as contact_2_name, CONCAT(contact_3.first_name, contact_3.last_name) as contact_3_name
FROM tasks
JOIN contacts contact_1 ON tasks.contact_id1 = contact_1.contact_id
JOIN contacts contact_2 ON tasks.contact_id2 = contact_2.contact_id
JOIN contacts contact_3 ON tasks.contact_id3 = contact_3.contact_id

But my issue here is that it is not showing tasks where any of the values of contact_id1-contact_id6 have a value of 0 which is the default value if no contact_id is set for it.

Any help you can provide would be great.

  • 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-10T12:53:49+00:00Added an answer on June 10, 2026 at 12:53 pm

    You can make a view that does normalize the tasks table then the join will be easy. You can make a permanent view or just for the select.

    The view would need somehow to be a UNION

    SELECT task_id, task_name, task_description, contact_id1 as contact_id, completed from tasks
    UNION ALL
    SELECT task_id, task_name, task_description, contact_id2              , completed from tasks
    UNION ALL
    SELECT task_id, task_name, task_description, contact_id3              , completed from tasks
    UNION ALL
    SELECT task_id, task_name, task_description, contact_id4              , completed from tasks
    UNION ALL
    SELECT task_id, task_name, task_description, contact_id5              , completed from tasks
    UNION ALL
    SELECT task_id, task_name, task_description, contact_id6              , completed from tasks
    

    So then it looks like this

    WITH tasks_easy as (    
        SELECT task_id, task_name, task_description, contact_id1 as contact_id, completed from tasks
        UNION
        SELECT task_id, task_name, task_description, contact_id2              , completed from tasks
        UNION
        SELECT task_id, task_name, task_description, contact_id3              , completed from tasks
        UNION
        SELECT task_id, task_name, task_description, contact_id4              , completed from tasks
        UNION
        SELECT task_id, task_name, task_description, contact_id5              , completed from tasks
        UNION
        SELECT task_id, task_name, task_description, contact_id6              , completed from tasks
     )
     select *
     from   tasks_easy t
     join   contacts c   on (t.contact_id = c.contact_id)
    

    Have not tested this, but should work like this.

    Edit: Actually was thinking about it. UNION ALL, at least for the temporary view is not necessary. Probably will bring you some time improvement if your tables are real large.

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

Sidebar

Related Questions

I need some help with query from multiple tables. My database: I have following
I need some help creating a query for my mySQL database. I have recently
Need some help with a query.. I have three tables. Source id name 1
I need some help with a LINQ query in VB.Net, please. I have this
I need some help a query. For each movie in my database that has
I need some help here i have this 2 tables : table clients +------+-------------+-----------+
I need some help on a MySQL query I'm trying to set up. I
I'm very new to MySQL so I need some help please, I have a
I really need some help with forming a MySQL query that I just cannot
I am new to SQL Server 2008, and I need some help in query

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.