retrive multiple columns as multiple columns in one row mysql [duplicate] - mysql

This question already has answers here:
MySQL - Rows to Columns
(13 answers)
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 5 years ago.
I am writing a query to get result from a table "tbl_meta" having 3 columns as
ID,attr_name,attr_value.
Table structure is like 2nd column is name of attribute and 3rd column is its value like below
ID attr_name attr_value
1 name abc
2 address aaa
3 age 25
So,when I use "select * from tbl_meta where ID = 10" , I got result like as it is as shown above but I need result with columns in a single row like below:
col1 col2 col3 col4 col5 col6
name abc address aaa age 25
how can I do this using mysql?
NOTE
I have tried solutions given # MySQL - Rows to Columns and MySQL pivot row into dynamic number of columns but it's not how I am expecting.
When I tried like
SELECT
case when attr_name = 'name' then attr_value end as name ,
case when attr_name = 'address' then attr_value end as address,
case when attr_name = 'age' then attr_value end as agep,
FROM tbl_meta
where ID = 10
I am getting output as :
name address age
abc NULL NULL
NULL aaa NULL
NULL NULL 25
instead can't I get like
name address age
abc aaa 25
from above table.

You can use something like;
SELECT *, (IF ID=10) AS new_column FROM tbl_meta;

Related

Insert data into a column only if it has NULL value, else update it [duplicate]

This question already has answers here:
MySQL on duplicate key update
(2 answers)
Closed 1 year ago.
I want to update data into a column in a table only if the data sent from the HTML page is not NULL or ''. Else, that column must not be touched. Example :-
table_1
id name age rent
==========================================
1 Name 1 25 1000
2 Name 2 28 NULL
3 Name 3 35 1500
4 Name 4 44 3200
5 name 5 42 NULL
LET THE DATA SENT FROM HTML PAGE IS STOERED IN A VARIABLE today_rent AND THIS VARIABLE IS SENT TO MYSQL.
MySQL Query
IF(today_rent !=null OR today_rent != '') THEN
UPDATE table_1 SET rent=today_rent WHERE id=2
END IF;
Is there any other way to do it?
Add an index
CREATE UNIQUE INDEX idx ON test (id, (CASE WHEN rent IS NULL THEN 0 ELSE id END));
Then use
INSERT INTO test (id, rent)
VALUES (#id,#new_rent)
ON DUPLICATE KEY UPDATE rent = VALUES(rent);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c48a44a6268e2e1710162b337efa592b
PS. The query does not set name and age columns - but their setting necessity is not described in the question.

MySQL Query to replace string with value

I have requirement like as below.
Need a MYSQL query to replace value with maching the below condition.
i have a table containg the Product ID
Product_ID
1
2
3
4
5
15
25
I want to replace the 5 with value of 1.111. My requiremnet is this that it should only replace the 5 value not the 15 value.
example 5 should be 1.111 but it sould not replace the 15 value.
You can use IF() or CASE to select a different value when the value meets a condition.
SELECT IF(product_id = '5', '1.111', product_id)
FROM yourTable
or
SELECT CASE product_id
WHEN '5' THEN '1.111'
ELSE product_id
END
FROM yourTable
CASE generalizes more easily to other values that you want to replace, since you can have multiple WHEN clauses.

Query which Find string and increment the count

I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..

Combine column results in SQL Server

I have a functionality where user can select table(only one at a time) and based on that table columns will appear. User can select multiple columns so what I want is,when user selects column(s) then it should return the result of selected columns. Now as per my table if user selects Colum_1 which has unique values then I'll allow user to select that but if user selects Column_2 I'll prompt the message by saying that 'Select another column or more than one', and then in background I'll create combination for those selected columns.
Example:
Table name = `TestTable`
Column_1 Column_2 Column_3
-------- -------- --------
1 1 ab
2 2 bc
3 1 bc
Expected results:
Column_1 = 1 2 3
Column_1, Column_2 = 1 1, 2 2, 3 1 (combination of two columns)
Column_1, Column_2, Column_3 = 1 1 ab, 2 2 bc, 3 1 bc (combination of three columns)
I've tried a query but it is useful for one column name only. I'm not sure if user selects multiple columns then how to handle that.
My query:
declare #colCount bigint, #uniqColCount bigint, #result nvarchar(max)
select #colCount = count(Column_1) from TestTable
select #uniqColCount = count(distinct Column_1) from TestTable
if(#colCount = #uniqColCount)
begin
set #result = (select Column_1 from TestTable)
print #result
end
else
print 'false'
I need to achieve for-each kind of logic in SQL.
You could use a While condition to solve this. Here is a reference for more details.
https://msdn.microsoft.com/en-CA/library/ms178642.aspx

update a part of field using matching id from another table

I have table with following data
(here key field comprises of site__marker)
id userid key value
1 1 site_1_name http://example1.com
2 1 site_1_user user1
3 1 site_2_name http://example2.com
4 2 site_1_name http://example3.com
5 2 site_1_user user2
and I have site mapping table
oldsiteid newsiteid
1 120
2 152
Now I need to update first table in way that only values updates in key field
should match oldsiteid in second table, and should get updated by newsiteid
Output should be like
id userid key value
1 1 site_120_name http://example1.com
2 1 site_120_user user1
3 1 site_152_name http://example2.com
4 2 site_120_name http://example3.com
5 2 site_120_user user2
How to achieve this?
You will have to translate this REXX in SQL, the functions are staight forward:
old = 'user_1_ddd'
n = "333"
new = substr(a,1,index(a,"_")) || n || substr(a,index(a,"_") + index(substr(a, index(a,"_")+1) ,"_"))
results in user_333_ddd
substr is the same in both
for index, use Find_in_set
for || use concat
I do not have MySQL but this should work:
UPDATE TargetTable
SET key = CONCAT
(
SUBSTRING_INDEX(key,'_',1)
,'_'
, (SELECT newsiteid FROM MappingTable WHERE MappingTable.oldsiteid = SUBSTRING_INDEX(SUBSTRING_INDEX(TargetTable.key,'_',-2), '_', 1 ))
,'_'
,SUBSTRING_INDEX(key,'_',-1)
)