I know how to select distinct rows, but I can't find how to extract distinct 'words/string/data' from a field to update another column.
For example, if I have data in a table like so..
Table 1
ID TEXTDATA
123 ROCK DANCE ROCK INDIE ROCK POP DISCO EURO POP
456 POP DANCE DISCO POP
I want to UPDATE another field in another table with 'distinct' data from within the TEXTDATA fields, so it would look like this
Table 2
ID NEWTEXTDATA
123 ROCK DANCE INDIE POP DISCO EURO
456 POP DANCE DISCO
I worked out how to Update one table with data from another...
UPDATE table2 JOIN table1 ON table2.ID = table1.ID SET table2.NEWTEXTDATA = table1.TEXTDATA;
But I want it to be of distinct values within the TEXTDATA records. Hope that makes some sort of sense. Is that possible to do within an update query?
Many thanks in advance
For Sql Server 2008:
You can do this by using the xml query with distinct values. The below code will give you the distinct records from given row.
cast(cast('<d>'+replace(TEXTDATA, ' ','</d><d>')+'</d>' as xml).query('distinct-values(/d)') as varchar(max))
So with the help of above function your final query will be something like below.
UPDATE
table2
SET
NEWTEXTDATA = cast(cast('<d>'+replace(TEXTDATA, ' ','</d><d>')+'</d>' as xml).query('distinct-values(/d)') as varchar(max))
FROM
table2 t2
JOIN
table1 t1
ON
t2.ID = t1.ID;
Here is the working SQLFiddle for same.
For MySql: There is no way in MySql to achieve this.
Normalize your database.
get values from table and use php explode() , and use array_unique to remove duplicate values.
Related
I need to replace part of a string with a value from another database table. Actually I need to replace the userids with emails.
DB1.TABLE1
ID|EMAIL
1 |johndoe; janedoe;
2 |otherguy; johndoe;
DB2.TABLE2
ID|USERID |EMAIL
1 |johndoe |johndoe#test.com
2 |janedoe |janedoe#test.com
3 |otherguy|otherguy#test.com
my query
UPDATE
TABLE1
set
EMAIL = TABLE2.EMAIL
from
DB2.TABLE2
where
TABLE1.EMAIL = TABLE2.USERID
How can I specify the "part of the string" thing ?
There are a number of comments about changing your schema...which would be the best way forward.
It looks like what you are storing in table1.email is actually a list of UserId from table2. So you'll need to break out these ids in order to join to the tables together.
If you absolutely must follow this path, then there are existing Q+As on the site that will help you:
(I've taken a leap of faith that you are using SQL server ... but if you search I'm sure you can find similar answers for other RDBMSs)
Turning a Comma Separated string into individual rows
and
Multiple rows to one comma separated value
I guess you need the following
UPDATE TABLE1
SET EMAIL = (
SELECT TABLE2.EMAIL
FROM TABLE2
WHERE TABLE1.EMAIL LIKE TABLE2.USERID + '%');
demo
I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.
I have what Im guessing I have a stupid question but I cant seem to get my update statement to work.
I have the following query.
SELECT REPLACE(CAST(Text_col AS NVARCHAR(250)),'Vegetable oils','Refined Vegetable oils')AS 'DESCRIPTION', Text_col as 'ORIGINAL'
FROM Table1
where doc_id in (SELECT DOC_ID
FROM Table1
where Text_col like '%Vegetable oils%')
and FUNCTION_CODE = 'INGREDIENT'
This Returns me a table with two columns. It has 200+ rows. The DESCRIPTION column has the TEXT field with the updated text and the ORIGINAL has the original text so I could see all the changes side by side. I was happy with how it looked so I tried the following Query to update the tale to implement the changes.
begin transaction
UPDATE Table1
Set Text_col = REPLACE(CAST(Text_col AS NVARCHAR(MAX)),'Vegetable oils','Refined Vegetable oils')
WHERE doc_id in(SELECT DOC_ID
FROM Table1
WHERE Text_col like '%Vegetable oils%')
and function_code = 'INGREDIENT DEC
The Query runs without error but updates 0 rows. For the life of me I can't spot why it wont update the 200+ rows that appear in the above SELECT statement.
Im running this on SQL 2012 Management studio as well if that is important.
Thanks
Pretty sure your entire update could be simplified to this.
UPDATE Table1
Set Text_col = REPLACE(CAST(Text_col AS NVARCHAR(MAX)),'Vegetable oils','Refined Vegetable oils')
where FUNCTION_CODE = 'INGREDIENT'
There really is no need to limit the update since it is going to have to scan the table anyway and the subselect to pull doc_id is just adding extra work.
I have a table that I need to Update by adding a new field.. I can alter the table and update each row . but is there a way of appending the result of a query to the table? ( I know that the result will have the same number of rows)
EDIT: So let me make it clear
I have
table1 | col1,col2
I generate another single column table
table2 | col1
I want
table3 | table1.col1,table1.col2,table2.col1
By the way table1 & table2 have no common fields so I cant join them meaningfully.
I think so. I haven't tested this, but what I can find it seems that you can use a SubQuery to do something along the lines of
UPDATE Table1 SET Column1 = Column1 & (SELECT Column2 FROM Table2 WHERE xxxx)
I have one mdb table with the following structure:
Field1 Field2 Field3 Field4
A ...
B ...
I try to use a query to list all the different fields of row A and B in a result-set:
SELECT * From Table1
WHERE Field1 = 'A'
UNION
SELECT * From Table1
WHERE Field1 = 'B';
However this query has two problems:
it list all the fields including the
identical cells, with a large table
it gives out an error message: too
many fields defined.
How could i get around these issues?
Is it not easiest to just select all fields needed from the table, based on the Field1 value and group on the values needed?
So something like this:
SELECT field1, field2,...field195
FROM Table1
WHERE field1 = 'A' or field1 = 'B'
GROUP BY field1, field2, ....field195
This will give you all rows where field1 is A or B and there is a difference in one of the selected fields.
Oh and for the group by statement as well as the SELECT part, indeed use the previously mentioned edit mode for the query. There you can add all fields (by selecting them in the table and dragging them down) that are needed in the result, then click the 'totals' button in the ribbon to add the group by- statements for all. Then you only have to add the Where-clause and you are done.
Now that the question is more clear (you want the query to select fields instead of records based on the particular requirements), I'll have to change my answer to:
This is not possible.
(untill proven otherwise) ;)
As far as I know, a query is used to select records using for example the where clause, never used to determine which fields should be shown depending on a certain criterium.
One thing that MIGHT help in this case is to look at the database design. Are those tables correctly made?
Suppose you have 190 of those fields that are merely details of the main data. You could separate this in another table, so you have a main table and details table.
The details table could look something like:
ID ID_Main Det_desc Det_value
This way you can filter all Detail values that are equal between the two main values A and B using something like:
Select a.det_desc, a.det_value, b.det_value
(Select Det_desc, det_value
from tblDetails
where id_main = a) as A inner join
(Select Det_desc, det_value
from tblDetails
where id_main = a) as B
on A.det_desc = B.det_desc and A.det_value <> B.det_value
This you can join with your main table again if needed.
You can full join the table on itself, matching identical rows. Then you can filter on mismatches if one of the two join parts is null. For example:
select *
from (
select *
from Table1
where Field1 = 'A'
) A
full join
(
select *
from Table1
where Field1 = 'B'
) B
on A.Field2 = B.Field2
and A.Field3 = B.Field3
where A.Field1 is null
or B.Field1 is null
If you have 200 fields, ask Access to generate the column list by creating a query in design view. Switch to SQL view and copy/paste. An editor with column mode (like UltraEdit) will help create the query.