简体   繁体   中英

get value by matching the header column name with row value in another table

Our client has an requirement of arranging the columns other than the fields coming from the database per client. When the client logs in, they will be able to see the columns as per their requirement with the value. So, we would like to compare the column name with the row value (stored as column name) from another table and get those value in a single array.

Let's say, I have 2 tables.

Table A is where all the data is fed

Table A
id    | job_no    | applied_date    | customer_id    |
------|-----------|-----------------|------------    |
1     | BOM/001   | 2019-06-18      | 1              |
2     | BOM/002   | 2019-08-16      | 1              |

Table B is where the custom field arrangement is stored

Table B
column_id    | field_name    | sort_order    | customer_id    |
-------------|---------------|---------------|----------------|
1            | job_no        | 1             | 1              |
2            | applied_date  | 2             | 1              |

The final result that the customer will see is

Final Result
job_no    | applied_date    |
----------|-----------------|
BOM/001   | 2019-06-18      |
BOM/002   | 2019-08-16      |

The query that we have tried is as below but returns on 1 value because of the limit 1

mysql

SELECT column_id
     , customer_id
     , field_name
     , sort_order
     , (SELECT CASE c.field_name WHEN 'job_no' 
                                 THEN d.job_no 
                                 WHEN 'applied_date' 
                                 THEN d.applied_date 
                                 ELSE 0 END 
          FROM table_a d 
         WHERE d.customer_id = c.customer_id LIMIT 1) AS value 
  FROM table_b c 
 WHERE c.customer_id = 1 
 ORDER 
    BY sort_order ASC");

The expected output shall be as final result which is shown above. Hope, I was able to explain you. Thank you in advance.

DROP TABLE IF EXISTS table_a;

CREATE TABLE table_a
(id SERIAL PRIMARY KEY
,job_no VARCHAR(12) NOT NULL
,applied_date DATE NOT NULL
,customer_id INT NOT NULL
);

INSERT INTO table_a VALUES
(1,'BOM/001','2019-06-18',1),
(2,'BOM/002','2019-08-16',1);

DROP TABLE IF EXISTS table_b;

CREATE TABLE table_b
(column_id SERIAL PRIMARY KEY
,field_name VARCHAR(20) NOT NULL UNIQUE
,sort_order INT NOT NULL
,customer_id INT NOT NULL
);

INSERT INTO table_b VALUES
(1,'job_no',1,1),
(2,'applied_date',2,1);

SELECT DISTINCT job_no 
              , applied_date 
           FROM table_a a 
           JOIN table_b b 
             ON b.customer_id = a.customer_id 
          ORDER 
             BY job_no;
+---------+--------------+
| job_no  | applied_date |
+---------+--------------+
| BOM/001 | 2019-06-18   |
| BOM/002 | 2019-08-16   |
+---------+--------------+

Perhaps the data set is not sufficiently representative of the problem.

If so, see: Why should I provide an MCRE for what seems to me to be a very simple SQL query?

You can join it with an IF condition:

SELECT
  b.id,
  a.customer_id,
  b.field_name,
  b.sort_order,
  IF(b.field_name = 'job_no', a.job_no, a.applied_date) value
FROM a 
INNER JOIN
    b ON b.customer_id = a.customer_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM