I want to delete all rows in a table that include a certain code in the column that contains the codes.
EX:
Name Code
John 3581
Alex 3132
Jake 2123
In another table, I have codes that correspond to a certain keyword. Some of the names have the code which corresponds to my keyword of choice that I want to eliminate. It look something like this
EX:
Code Keyword
3132 apple
2123 apple
4921 banana
Let's say I want to only screen out apple from the table of names. How would I do that?
I tried setting up a loop, but I guess you cant do that in MS Access. Also, I wanted to do try a WHERE statement.
This is what I had in mind
DELETE table1 where table1.numbers = table2.numbers;
I am simply not sure how to execute this code.
If you want to delete from table1 the rows with Code that in table2 have the Keyword apple, you can do it with EXISTS:
DELETE FROM table1 t1
WHERE EXISTS (
SELECT 1 FROM table2 t2
WHERE t1.Code = t2.Code AND t2.Keyword = 'apple'
)
You can also use in to avoid the correlated subquery:
delete from table1 t1 where t1.code in
(select t2.code from table2 t2 where t2.keyword = 'apple')
Here, table1 is the table containing Name & Code and table2 is the table containing Code & Keyword - change these table names to suit your data.
Related
I have 2 tables column col1 of table1 has values abc, def, ghi
The value of the column col2 of the second table contains one of the above values.
Now I want the list of all rows of table2 which are the part of the value of col1
Please use below syntax to get your data,
select t2.* from table1 t1
inner join table2 t2
on (t1.col1 = t2.col2);
Also please tag appropriate tag as well as provide sample data to make every one understand and get proper solution
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.
I have two tables in my SQLite Database (dummy names):
Table 1: FileID F_Property1 F_Property2 ...
Table 2: PointID ForeignKey(fileid) P_Property1 P_Property2 ...
The entries in Table2 all have a foreign key column that references an entry in Table1.
I now would like to select entries from Table2 where for example F_Property1 of the referenced file in Table1 has a specific value.
I tried something naive:
select * from Table2 where fileid=(select FileID from Table1 where F_Property1 > 1)
Now this actually works..kind of. It selects a correct file id from Table1 and returns entries from Table2 with this ID. But it only uses the first returned ID. What I need it to do is basically connect the returned IDs from the inner select by OR so it returns data for all the IDs.
How can I do this? I think it is some kind of cross-table-query like what is asked here What is the proper syntax for a cross-table SQL query? but these answers contain no explaination of what they are actually doing so I'm struggeling with any implementation.
They are using JOIN statements, but wouldn't this mix entries from Table1 and Table2 together while only checking matching IDs in both tables? At least that is how I understand this http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
As you may have noticed from the style, I'm very new to using databases in general, so please forgive me if not everything is clear about what I want. Please leave a comment and I will try to improve the question if neccessary.
The = operator compares a single value against another, so it is assumed that the subquery returns only a single row.
To check whether a (column) value is in a set of values, use IN:
SELECT *
FROM Table2
WHERE fileid IN (SELECT FileID
FROM Table1
WHERE F_Property1 > 1)
The way joins work is not by "mixing" the data, but sort of combining them based on the key.
In your case (I am assuming the key field in Table 1 is unique), if you join those two tables on the primary key field, you will end up with all the entries in table2 plus all corresponding fields from table1. If you were doing this:
select * from table1, table2 where table1.fieldID=table2.foreignkey;
then, providing your key fields are set up right, you will end up with the following:
PointID ForeignKey(fileid) P_Property1 P_Property2 FileID F_Property1 F_Property2
The field values from table1 would be from matching rows.
Now, if you do this:
select table1.* from table 1, table2 where
table1.fieldID=table2.foreignkey and F_Property1>1;
Would essentially get the same set of records, but will only show the columns from the second table, and only those that satisfy the where condition for the first one.
Hope this helps :)
If I understood your question correctly this will get the job done.
Select t2.*
from table1 t1
inner join table2 t2 on t2.id = t1.id
where t1.Prop = 'SomeValue'
I have created a new column in my table(table1) . I am trying to populate it with data from another table, table2.
Table1 has a column called 'Name'. 'Name' contains a substring indicating the language of the column. I wish to compare this substring with the 'Language' column of table2, which contains the substring in the name column and insert the corresponding LanguageID into my new column.
So, for instance :
table1
Name
xxXxxxXxxxxxzxzxzxz xxxazxzxxXXXZxxzxzx 2183909213 ENG-UK nfjksdnfnd 723984782347
and table2 :
table2
Language | ID
ENG-uk | 1
In the table1 name column, the string before and after the Language can take any form, a varying number of characters. The language will always have a space before and after it.
So, I want to end up with :
table1
Name | LanguageID
xx... | 1
I have this query which I believe should work :
INSERT INTO table1 (LanguageID)
SELECT t2.ID FROM table2 t2, table1 t1 WHERE CHARINDEX(LOWER(t2.Language), LOWER(t1.Name)) != null
The problem is, when I run this...."(0 row(s) affected)", which should not be the case.
Does anyone have any ideas ?
The reason that you don't get any matches at all is that you can't use the != operator to compare null values, you have to use is not null for that.
However, that will give you a very big result, as the return value from charindex is never null. When the string isn't found it returns zero, so that is what you should compare against.
Also, you can't insert columns, you have to first add the column to the table, then update the records:
update t1
set LanguageID = t2.ID
from table1 t1
inner join table2 t2 on charindex(lower(t2.Language), lower(t1.Name)) != 0
1st, CHARINDEX returns 0 when search string does not exist. It doesn't do null.
See http://msdn.microsoft.com/en-us/library/ms186323.aspx
2nd, I think you should UPDATE, not INSERT.
example (this won't work correctly if t2.Language is not UNIQUE):
UPDATE table1 t1
SET t1.LanguageID = (SELECT t2.ID from table2 t2 where CHARINDEX(LOWER(t1.Name), LOWER(t2.Language))>0)
where exists (SELECT t2.ID from table2 t2 where CHARINDEX(LOWER(t1.Name), LOWER(t2.Language))>0)
You should consider adding individual columns for the first table information. Otherwise, you will end up with Performance Issues due to the SubString Operation.
It's clear from the first table that the table schema is not Normalized. Moreover, the First table schema is not suitable for any Search/Sorting operations.
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)