Select column names [duplicate] - 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

How do i show the name of a column as a row data using a sql query?

I am quite inexperienced with the query writing.
I have a table like this,
+----+--------+------------+------------+------------+
| id | name | Character1 | Character2 | Character3 |
+----+--------+------------+------------+------------+
| 1 | A | 1 | 0 | 0 |
+----+--------+------------+------------+------------+
| 2 | B | 0 | 1 | 0 |
+----+--------+---------------+---------+------------+
i want to make a query where the result would show me like this, where i put a condition to search a name,
+----+--------+---------------+
| id | name | Character |
+----+--------+---------------+
| 1 | A | Character 1 |
+----+--------+---------------+
What will be the query for this?
There you go
select id, name, case 1
WHEN characters1 = 1 THEN characters1
WHEN characters2 = 1 THEN characters2
WHEN YourColumn = 1 THEN YourColumn //if you have more
Else 'no 1' END
from YourTableName where name LIKE '%searhname%'
Replace YourTableName above as your real table name, meanwhile searchname is what you or user is going to type in and search
YourColumn will be character2 and 3... or many more :)
For more information about LIKE, such as the difference between '%name' and 'name%', you may view this link here
You need a CASE expression:
select id, name,
case 1
when Character1 then 'Character1'
when Character2 then 'Character2'
when Character3 then 'Character3'
end Character
from tablename

Count all same comma separated value in one column mysql

I have 2 table and there's a column which contains comma separated value and I want to count all same value of that column, here's the sample table:
Client table
ID | Name | Procedure
1 | Joe | Samp1,Samp2
2 | Doe | Samp1,Samp2,Samp3
3 | Noe | Samp1,Samp2
Desire Output
Summary table ( For Procedure )
ID | NAME | COUNT
1 | Samp1 | 3
2 | Samp2 | 3
3 | Samp3 | 1
Now, do you have any idea or suggestion so i can make it happen ? like add new table or is this possible with single query ?
Try this query
SELECT LENGTH(COLUMN) - LENGTH(REPLACE(COLUMN, ',', '')) FROM TABLE_NAME

select Column names having a fixed value for any particular row

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';

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

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...