select Column names having a fixed value for any particular row - mysql

I am trying to make a query in mysql to get any column which has a particular value for one specific row.
In Mysql we can get rows based upon any specific value of a column.
I have a table like :
+----+------------+------------+---------------+---------------+---------+----------------+---------
| ID | MSISDN | MissedCall | SponsoredCall | AdvanceCredit | ACvalue | SuitablePackId | AutoTimeStamp |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
| 1 | 9944994488 | 1 | 0 | 1 | 0 | 1 | 2014-09-18 10:42:55 |
| 4 | 9879877897 | 0 | 1 | 0 | 0 | 2 | 2014-09-18 10:42:55 |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
What i need is when i select a row based upon MSISDN , it should return all column names for that row whose value is fix (say 1).
So in above table for MSISDN = 9944994488 it should return
MissedCall
AdvanceCredit
SuitablePackId
What i have tried is :
SELECT COLUMN_NAME as names
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'bi'
AND TABLE_NAME = 'useranalysisresult'
This returns me column names of table.
But how to get column names with specific value.
Thanks for help in advance.

This is too long for a comment.
A SQL query returns a fixed set of columns. You cannot change the set depending on the row. You could do what you want using a prepared statement, although that would seem like an arcane approach.
You could return a single column with values concatenated together. Something like:
select concat_ws(',',
(case when MissedCall = 1 then 'Missed Call' end),
(case when SponsoredCall = 1 then 'Sponsored Call' end),
. . .
)
from useranalysisresult;
This would produce a list in a single column of the flags being set.

Try the below one. Here replace the YOUR_SCHEMA with your actual schema name and YOUR_TABLENAME with your actual table name:
select column_name from information_schema.columns
where YOUR_SCHEMA = //any condition
and table_name='YOUR_TABLENAME';

Related

Select column names [duplicate]

I am trying to make a query in mysql to get any column which has a particular value for one specific row.
In Mysql we can get rows based upon any specific value of a column.
I have a table like :
+----+------------+------------+---------------+---------------+---------+----------------+---------
| ID | MSISDN | MissedCall | SponsoredCall | AdvanceCredit | ACvalue | SuitablePackId | AutoTimeStamp |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
| 1 | 9944994488 | 1 | 0 | 1 | 0 | 1 | 2014-09-18 10:42:55 |
| 4 | 9879877897 | 0 | 1 | 0 | 0 | 2 | 2014-09-18 10:42:55 |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
What i need is when i select a row based upon MSISDN , it should return all column names for that row whose value is fix (say 1).
So in above table for MSISDN = 9944994488 it should return
MissedCall
AdvanceCredit
SuitablePackId
What i have tried is :
SELECT COLUMN_NAME as names
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'bi'
AND TABLE_NAME = 'useranalysisresult'
This returns me column names of table.
But how to get column names with specific value.
Thanks for help in advance.
This is too long for a comment.
A SQL query returns a fixed set of columns. You cannot change the set depending on the row. You could do what you want using a prepared statement, although that would seem like an arcane approach.
You could return a single column with values concatenated together. Something like:
select concat_ws(',',
(case when MissedCall = 1 then 'Missed Call' end),
(case when SponsoredCall = 1 then 'Sponsored Call' end),
. . .
)
from useranalysisresult;
This would produce a list in a single column of the flags being set.
Try the below one. Here replace the YOUR_SCHEMA with your actual schema name and YOUR_TABLENAME with your actual table name:
select column_name from information_schema.columns
where YOUR_SCHEMA = //any condition
and table_name='YOUR_TABLENAME';

Retrieve rows in which a column doesn't contain a value

I have a MySQL table like below:
| ID | userIDs
---------------
| 1 | 4,3,5
| 2 | 2,3
| 3 | 1,2,3
I want to retrieve all the rows in which userIDs doesn't contain 1.
I tried
SELECT * FROM tablename WHERE 1 NOT IN (userIDs)
But it's not working.
Use FIND_IN_SET
SELECT * FROM tablename
WHERE find_in_set(1, userIDs) = 0
But actually you should rather change your table design. Never store multiple values in a single column!

Get column name dynamically by Specific row value

I am struck at writing a query.
Here I want to show the column name based on some specific value
For Instance, my table is like this:
id | fruits |vegetables |softdrink
-----------------------
1 | apple | Onion | Pepsi
2 | mango | Potato | Coke
3 | banana | Bringal | RedBull
If I have a value "mango", then I should get the column name as fruit or
If I have a value "RedBull", then I should get the column name as softdrink
NOTE: I have many columns around 48 to get the name from any one of them
set #q= CONCAT('SELECT columns.column_name
from table inner
join information_schema.columns
on columns.table_schema = "dbname"
and columns.table_name = "table"
and ((',
(SELECT GROUP_CONCAT(CONCAT('columns.column_name="',column_name,'"',' and table.',column_name,' = "value','"') SEPARATOR ' OR ')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table'),
'))');
prepare query from #q;
execute query;
This works for sure..
Phew!
Fiddle: http://sqlfiddle.com/#!2/9420c/2/2
PS: Replace table with your table name ,dbname with your db name and value with your value
As you encrypted your question as much as possible, I can only guess.
this table smells of a bad design. And it should be like
col | value
col1 | a
col1 | b
col2 | d
and so on.
than you can easily get your col out from value

How to loop a column data with comma separator using MYSQL

id | value
1 | 0, 1
2 | 0,1,2
table_1
I have a table known as table_1 which have id and value column.
I would like to loop through the value column for each data which is seperated with comma.
Then, I would like to insert the every data into a new table.
As below:
id | value
1 | 0
1 | 1
2 | 0
2 | 1
2 | 2
table_2
This means that in table_2, it will have two rows of when user_id=1.
Example: user_id = 1, value = 0
user_id =1, value = 1
How to achieve this? Your response is very much appreciated.
In fact you want the opposite of GROUP_CONCAT() function.
I found this, that might work for you : http://thenoyes.com/littlenoise/?p=92
Hope that helps.

SQL: Selecting columns based on column value from another table

I have the following tables:
UserPrivileges:
+--------+------+------+------+
| UserID | Col1 | Col2 | Col3 |
+--------+------+------+------+
| 1 | 0 | 1 | 1 |
| 2 | 0 | 0 | 1 |
| 3 | 1 | 0 | 0 |
| 4 | 1 | 1 | 0 |
+--------+------+------+------+
Data:
+--------+------+------+------+
| DataID | Col1 | Col2 | Col3 |
+--------+------+------+------+
| 1 | A | B | C |
| 2 | D | E | F |
| 3 | G | H | I |
| 4 | J | K | L |
+--------+------+------+------+
My question at its simplest form doesn't has anything to do with the Data table but I just explain it anyways so that I might be doing it the wrong way.
How would I select the Column names from UserPrivileges based on the value ? So that I can use the result in another query to select only those columns.
Something along these lines:
SELECT (COLUMNS_NAME_QUERY_FROM_UserPrivileges(UserID='#')) WHERE DataID = '#' FROM Data
Or I don't mind a better way to manage user Privileges for specific columns.
The answer depends upon your requirements for the result. Do you require a result with a consistent set of columns, regardless of user privs? If so, you could set the disallowed values to null (or some other special value) using a IF clause, e.g.,
SELECT IF (p.col1 = 0 THEN NULL ELSE d.col1) AS col1,
IF (p.col2 = 0 THEN NULL ELSE d.col2) AS col2,
IF (p.col3 = 0 THEN NULL ELSE d.col3) AS col3
FROM Data d,
UserPrivileges p
WHERE p.userId = '#'
AND d.DataId = '#'
Of course, the "special value" could be a problem, since you need a value that would never appear in the data. If you needed to know that difference between a null because the real value is null vs. null because it is a prohibited column then you can't use null.
Another approach would have you simple include the privilege indicator for each column appear in the result, and let your business logic use that to determine which values are visible to the user.
A very different approach would have the result set to contain only the allowed columns. In this case you'll need to build your sql statement dynamically. I don't know if you are doing this in a stored procedure or in a host language, but the basic idea is something like this:
string sqlCmd = "SELECT "
+ (SELECT (FIELDS_NAME_QUERY(UserID='#')
FROM USER_PRIVILEGES
WHERE userid='#')
+ FROM data d
execute sqlCmd
"execute" meaning whatever you have available to execute a string as a sql command.
more after clarification by OP:
Ok, you need sql function that returns a string that looks like "colname1, colname2, ...". The following resembles what it would look like in sql server. syntax
create function
FIELDS_NAME_QUERY (#userid int)
begin
select col1, col2, col3... INTO #col1priv, #col2priv, #col3priv FROM userPrivileges WHERE UserId = #UserId
declare #result varhcar(60)
set #result = ''
if (#col1priv = 1) #result = 'col1'
if (#col2priv = 1) #result = #result + ' ,col2'
if (#col3priv = 1) #result = #result + ' ,col3'
return #result
end
have not tried it, but something like this should work
SELECT (SHOW COLUMNS FROM table WHERE expr) FROM data WHERE DataID = '#'
Check this post for details - How can I get column names from a table in Oracle?
Let us know how you solve this...