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
Related
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';
My table structure is as follows,
ID Name Source
1 John first.jpg
2 Doe second.jpg
3 Mary third.jpg
4 Kurian four.jpg
I would like to update the "Source" by prepending with the host and primary key as follows
http://example.com/1/first.jpg
http://example.com/2/second.jpg
http://example.com/3/third.jpg
http://example.com/4/four.jpg
tried with CONCAT("http://example.com/"+id,Source) but fails with Truncated incorrect DOUBLE value:
Any suggestion will be greatly apreciated.
Try
UPDATE table_name
SET Source = CONCAT('http://example.com/', ID, '/', Source);
Result
| ID | Name | Source |
|----|--------|---------------------------------|
| 1 | john | http://example.com/1/first.jpg |
| 2 | Doe | http://example.com/2/second.jpg |
| 3 | Mary | http://example.com/3/third.jpg |
| 4 | Kurian | http://example.com/4/fourth.jpg |
checkout this
SELECT CONCAT("http://example.com/" , CONCAT(ID , CONCAT("/" , Source))) FROM table_name;
or simply
SELECT CONCAT("http://example.com/" ,ID , "/" , Source) FROM table_name;
You can iterate in sql but the easiest way is to create a php script in which you create an array with the rows of the table
Then use the dot si tax to concat ID with the domain name and the source, something like:
$newval = ‘{$table[“ID”]}’ .“/Domainname.com/” . ’{$table[“Source”]}’;
Where $table is a variable storing the associative array of the table
And then make a new query in which you override the source column for each row in the table array
Please try below code:
UPDATE table_Name
SET Source = concat(concat(CONCAT('http://example.com/',ID),'/'),source);
Suppose i have a table
table
+-------------+
| id | name |
+-------------+
| 1 | xabx |
| 2 | abxd |
| 3 | axcd |
| 4 | azyx |
| 5 | atyl |
| 6 | aksd |
| 7 | baabc|
| 8 | aabcd|
+-------------+
first i have to get data if matches first some char like :
if name = aab
then have to run select * from table where name like 'aab%'
then it returns
+-------------+
| 8 | aabcd |
+-------------+
which execatlly i want
but if i have only abc
then the above query return 0 row
then i have to search from middle like :
select * from table where name like '%abc%'
then it returns which is the alternative
+-------------+
| 7 | baabc|
| 8 | aabcd|
+-------------+
i have no much knowledge about mysql is there any query which can do like if first where condition don't have row then run alternative where condition
i have tried this but didn't work as i want.
select * from table where name like 'abc%' or name like '%abc%'
fiddle
thanks in advance
This is somewhat your desired result:
select *from t
where (case
when name like 'abc' then 1
when name like 'bc%' then 1
when name like '%bc' then 1
when name like '%bc%' then 1
else null
end)
order by name
limit 1;
I just put all the combinations as conditions.
You can interchange their sequence or remove unnecessary condition.
limit 1 makes only 1 row visible for whichever condition satisfies.
Here is the answer from your fiddle. Check it out
Hope it helps!
This is a possible solution:
Left joining the table on itself, where the table is initially filtered by the more inclusive %bx% and then the join is filtered by the more restrictive bx%.
This allows you to use the joined name if it exists, but revert to the original if not:
SELECT t1.id, IF(t2.name IS NULL, t1.name, t2.name) name
FROM test t1
LEFT JOIN test t2 ON t2.id = t1.id AND t1.name like 'bx%'
WHERE t1.name LIKE '%bx%'
This may/may not be ideal depending on the size or your dataset.
COUNT checking may work
select *
from table
where name like 'aab%' or
((select count(*) from table where name like 'aab%') = 0 and name like '%abc%')
I guess that it would be a good idea to compute the count value into a variable first, however, the optimizer may recognize independent subquery anyway and run it once.
I have the following tables:
cars
id
name
color
bicycles
id
name
number_of_gearshift
I need a central index table of this tables in my mysql database and a unique id for them. Something like this:
items
id
table_name
Lets say, the id in the items-table is the same as in the corresponding table:
items
id | table_name
1 | cars
2 | cars
3 | bicycles
4 | cars
cars
id | name | color
1 | Peugeot | red
2 | BMW | green
4 | Nissan | blue
bicycles
id | name | number_of_gearshift
3 | Stevens | 24
My question - the following situation:
I have the ID (for example XXX) of an item. Now I want to get the data of this item, by only one query. Something like this (I know, that will not work):
SELECT table2.*
FROM (SELECT table_name FROM items WHERE id = XXX) AS table2
Is it possible?
Use can use a dynamic sql query to achieve this.
set #query = null;
set #id = 3;/*change according to requirement*/
SET #tn := (select `table_name` from items where id = #id);
set #query = concat('select * from ',#tn,' where id = ',#id);
prepare stmt from #query;
execute stmt;
deallocate prepare stmt;
Change the value of #id according to your requirement.
SQL Fiddle
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';