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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:04:08+00:00 2026-06-17T08:04:08+00:00

I am trying to create a derived table from a few different tables, everything

  • 0

I am trying to create a derived table from a few different tables, everything works if i do single inner join, but when i add more joins i can’t get the desired result. Here is what i got now:

Table structure:

First table:

+------------+------------+------+-----+---------+-------+
| Field      | Type       | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| TAG_ID     | int(11)    | NO   | PRI | NULL    |       |
| STATUS     | int(11)    | YES  |     | NULL    |       |
| ENABLE     | int(11)    | YES  |     | NULL    |       |
| TIME_STAMP | bigint(20) | YES  |     | NULL    |       |
| VALUE      | float      | YES  |     | NULL    |       |
+------------+------------+------+-----+---------+-------+

Second table:

+--------------------+---------------------+------+-----+------------+----------------+
| Field              | Type                | Null | Key | Default    | Extra          |
+--------------------+---------------------+------+-----+------------+----------------+
| TAG_ID             | int(11)             | NO   | PRI | NULL       | auto_increment |
| TAG_DEV_ID         | int(11)             | NO   |     | NULL       |                |
| TAG_NAME           | varchar(256)        | NO   |     | NA         |                |

.....................

| TAG_TIME_STAMP     | bigint(20) unsigned | NO   |     | 0          |                |
+--------------------+---------------------+------+-----+------------+----------------+

Here is what i do now – first of all i create a view of both tables after joining them into one big table:

CREATE VIEW U1_TEMP_TEST AS (SELECT TAG_ID AS TID, VALUE FROM table_1);
CREATE VIEW U1_TEST AS (SELECT * FROM table_2 AS T2 INNER JOIN U1_TEMP_TEST AS T1 ON T2.TAG_ID = T1.TID);

Now what i need is to join the table to itself so i could get rows from ‘VALUE’ in different COLUMNS, i do it like this:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE 
FROM U1_TEST AS U1 
INNER JOIN ((SELECT * FROM U1_TEST 
        WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2) 
        ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
WHERE (U1.TAG_ID = 4 OR U1.TAG_ID = 20 OR U1.TAG_ID = 14) 
GROUP BY U1.TAG_DEV_ID;

and i get good result like that:

+------------+--------+--------+-------+
| TAG_DEV_ID | TAG_ID |  VALUE | VALUE |
+------------+--------+--------+-------+
|          1 |      4 |   -0.5 |   2.1 |
|          2 |     14 |  -12.7 |   0.4 |
|          3 |     20 |   -5.7 |   9.5 |
+------------+--------+--------+-------+

But if i try adding one more join like that:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE, U3.VALUE 
FROM U1_TEST AS U1 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2) 
    ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 3 OR TAG_ID = 13 OR TAG_ID = 22)) AS U3) 
    ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
WHERE U1.TAG_ID = 4 OR U1.TAG_ID = 14 OR U1.TAG_ID = 24 
GROUP BY U1.TAG_DEV_ID;

the data returned is not what i expect, not all values are selected.
Maybe there is something wrong with the second inner join, i’m stuck with this for hours and can’t understand why the single join query works fine and the last one doesn’t.

Thanks for any help !

EDIT result data:

+------------+--------+-------+-------+-------+
| TAG_DEV_ID | TAG_ID | VALUE | VALUE | VALUE |
+------------+--------+-------+-------+-------+
|          1 |      4 |  -0.5 |     3 |     0 |
|          2 |     14 | -12.7 |   0.5 |   1.6 |
+------------+--------+-------+-------+-------+

and i expect it to be something like that:

+------------+--------+-------+-------+-------+
| TAG_DEV_ID | TAG_ID | VALUE | VALUE | VALUE |
+------------+--------+-------+-------+-------+
|          1 |      4 |  -0.5 |     3 |     0 |
|          2 |     14 | -12.7 |   0.5 |   1.6 |
|          3 |     21 |     x |     x |     x |
+------------+--------+-------+-------+-------+
  • 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-17T08:04:09+00:00Added an answer on June 17, 2026 at 8:04 am

    Use LEFT JOIN like so:

    SELECT 
      U1.TAG_DEV_ID, 
      U1.TAG_ID, 
      U1.VALUE, 
      U2.VALUE, 
      U3.VALUE 
    FROM U1_TEST AS U1 
    LEFT JOIN U1_TEST AS U2  ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
                            AND U3.TAG_ID IN(1, 12, 21) 
    LEFT JOIN U1_TEST AS U3  ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
                            AND U3.TAG_ID IN(3, 13, 22) 
    WHERE U1.TAG_ID IN(4, 14, 24)
    GROUP BY U1.TAG_DEV_ID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a single column picker whose data is derived from
I was trying to create ListCtrl-derived class to add 2 functions, set_data and _set_column_width
I am trying to create a new integer array which is derived from a
I'm trying to create a comma separated list and I'm using a derived table.
I am trying to create a Custom control derived from a standard Grid. I
I am trying to create a derived class object in base class method. I
Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to write a factory method that will create a derived instance

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.