Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL query to get column name where my search condition meets

I have a table in a MySQL database. I am given a value that occurs as a cell value in that table but I want to find the column name where my search condition meets. What is the most efficient way to find the column to which that value belongs?

But my search query to find the column name WHERE user4 exist and module_name = ‘accounts’. I am trying below but not working –

SELECT col FROM (
   SELECT "view_only" AS col, view_only AS value FROM module_access
   UNION ALL SELECT "limited_access", limited_access FROM module_access
   UNION ALL SELECT "full_access", full_access FROM module_access
) allValues
WHERE (value LIKE '%user4,%') AND (module_name = 'accounts');

My Sample Table Structure like below-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Table Name: module_access

module_name  |  view_only     |  limited_access  |  full_access
============================================================================
accounts     |  user1,user2,  | user3,user4,     | superadmin,admin,
----------------------------------------------------------------------------
sales        |  user1,user4,  | user3,           | superadmin,admin,user2, 
============================================================================

I refer this Find column that contains a given value in MySQL and use below query and works but it is not the complete solution I want.

SELECT col FROM (
   SELECT "view_only" AS col, view_only AS value FROM module_access
   UNION ALL SELECT "limited_access", limited_access FROM module_access
   UNION ALL SELECT "full_access", full_access FROM module_access
) allValues
WHERE value LIKE '%user4,%';

>Solution :

Your intent was that you first query would return "limited_access", but instead it returned an error ("Unknown column ‘module_name’ in ‘where clause’")

Fixing this problem:

SELECT col FROM (
   SELECT module_name, "view_only" AS col, view_only AS value FROM module_access
   UNION ALL 
   SELECT module_name, "limited_access", limited_access FROM module_access
   UNION ALL 
   SELECT module_name, "full_access", full_access FROM module_access
) allValues
WHERE (value LIKE '%user4,%') AND (module_name = 'accounts');

now this query returns: limited_access

see: DBFIDDLE

But, as others noted, your database design is not correct.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading