Suppose the first column is A and the second column is B in the picture below.
If there are duplicate values in column A, I want to remove the row which has the "*" appended to it in column B.
This would result in only the first row in the resultant table.
There are multiple rows like this in my MS Access query. How can I go about removing the appropriate ones in the design view?
This should do the trick:
DELETE DISTINCTROW T1.*
FROM Table1 T1 INNER JOIN
(
SELECT FieldA
FROM Table1
GROUP BY FieldA
HAVING Count(FieldA)>1
) T2 ON T1.FieldA = T2.FieldA
WHERE INSTR(T1.FieldB,"*")>0
Related
E.g. in Pandas, we can apply a mask and create a new dataframe and assign it a name. Similarly in SQL, once I do a LEFT JOIN of 2 tables, is there a way to refer to the new combined table ?
You can join two table and can get the result in the new combined and also you can give name to that table . Just try this query and if get any doubt just feel free to ask anytime.
MYSQL QUERY
EMP(C1, C2, CD1)
DEPT(D1, D2)
SELECT NEWTABLE.First, NEWTABLE.Third
FROM
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1) NEWTABLE
WHERE NEWTABLE.Second > 20;
We have created a virtual table i.e "NEWTABLE" you can give your name also .
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1)
This is the query for where we have applied join query and also we have selected the three row from two table and renamed it as "FIRST", "SECOND" and "THIRD".
And you will get the doubt in the first line so let me clear we have performed the operation NEWTABLE.Second > 20;on the new table which we obtained after join.
If you still get any doubt regarding the query just ask .
Values Stored in the new table is temporary and you can use it for that query only.
And if you want to store permanent value then you have create to new table then assigned that table with the table we joined and so on .
No that won't work in sql, at least not directly
But you can do a subquery
Like
SELECT aa.*
FROM
(SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid) aa
or A view
CREATE VIEW v AS SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid;
A problem can result, when you have in both tables the same names for columns, that would cause problems, so you must check and in case of equal columnames alias the second column
I am fairly new to ms access (working with access 2013) and unfortunately am stuck with a problem.
I am currently working on an update query with 2 tables. In table 1 I would like to update all fields of a column with a "1" based on multiple criteria. Three different criteria exist in both tables. I only want to update the column if 2 of the criteria are exactly the same in both tables and one criteria is larger in table 2 than in table 1. However, unfortunately even if all criteria match, that does not mean that the certain case is unique. However, I just want to Update a "1" only once per unique row of table 2.
So basically, I have to questions:
Is the current code correct concerning the match I want to make?
Is there any way to tell access to only update once per unique row in table 2?
Thanks a lot for your help!
This is my current code:
UPDATE Table2 LEFT JOIN [Table1] ON (Table2.Criteria1 = [Table1].Criteria1) AND (Table2.[Criteria2] = [Table1].[Criteria2]) SET [Table1].Column = 1
WHERE (((Table2.[Criteria1])=[Table1].[Criteria1]) AND ((Table2.Criteria2)=[Table1].[Criteria2]) AND ((Table2.Criteria3)>=[Table1].[Criteria3]));
A left join and a where on same table work as an inner join. Looking to your code seems you need a join between table1 and table2 for update table2. So the syntax and the condition should be:
UPDATE Table2
SET [Table1].Column = 1
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
But if you need an update only for a single rows the you could trying using the min(id) resulting from the matching row:
UPDATE Table1
SET [Table1].Column = 1
WHERE Table1.ID = (
SELECT MIN(ID)
FROM Table2
INNER JOIN [Table1] ON Table2.Criteria1 = [Table1].Criteria1
AND Table2.[Criteria2] = [Table1].[Criteria2]
AND Table2.Criteria3>=[Table1].[Criteria3]
)
How would I set a condition between 2 tables?
1 table has IDs whose values that need to be changed based on values set in another table.
The query I have so far...no idea if it works.
UPDATE table1 set value='2' INNER JOIN table2 WHERE CONVERT(value USING utf8) LIKE '%text%') so when value is 2 in the first table, that same item in table 2 will be assigned a specific category. The query should check that the ID in table 2 is the same ID that it found in table 1 that contained the value.
Your code looks like MySQL. If so, the correct syntax is more like this:
UPDATE table1 t1 INNER JOIN
table2 t2
ON t1.id = t2.id
set t1.value = '2'
WHERE CONVERT(t1.value USING utf8) LIKE '%text%');
Your question is a bit vague on the actual JOIN conditions, but that is the structure of the query.
I have this MySQL Update statement. It works fine.
UPDATE Table1
SET Table1_field1='field1_content', Table1_field2='field2_content'
where Table1_field3=2
All the fields above belong to the same table. I then added an extra condition AND Table2.fieldname='XXX' to the WHERE clause
UPDATE Table1
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND Table2.fieldname='XXX'
This time, the SQL statement fails. The error is "unknown column Table2.fieldname in where clause". However, Table2.fieldname does exist.
In order to be able to use fields from Table2 in your query you'll need use a JOIN between Table1 and Table2.
A JOIN effectively combines a row from each table into a single row for your query, based on a provided condition.
For example if both Table1 and Table2 have a column tableID, we can combine rows from each table where the tableIDs match.
The query would then look like below:
UPDATE Table1
JOIN Table2
ON Table1.tableID = Table2.tableID
SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
WHERE Table1_fieldname3=2 AND Table2.fieldname='XXX';
The JOIN keyword is equivalent to INNER JOIN. There are different types of JOINs available and I'd recommend reading up about them.
Here's a reference image to give you an idea of the different types:
you need to join table 1 and table2; then you can update
UPDATE Table1 AS b
INNER JOIN Table2 AS g ON b.id = g.id SET Table1_fieldname1='field1_content', Table1_fieldname2='field2_content'
where Table1_fieldname3=2 AND g.fieldname='XXX'
I need to select the data from two tables and insert into one table. The same kind of the question was asked and answered many times but I have kind of requirement.
I have total three tables T1,T2,T3.
My final goal is to insert the data into table T3. I have total 15 columns in table T3. Among that 15 columns I need to fill the 14 columns data from table T2 and the data for the last column I need to join table T1 and T2 from that I need to fetch the data for that column. Please find the below query
CREATE procedure proc_name
BEGIN
Insert into T3(
id,
col1,
col2,
....
...
col14)
select
(select id from T1 INNER JOIN T2 ON
(T1.somecol1=T2.somecol1,
T1. somecol2= T2.somecol2,
T1.somecol3 = T2.somecol3,
T1.somecol4= T2.somecol4)
ORDER BY T2.somecol5 LIMIT 1),
T2.col1,
T2.col2,
...
...
T2.col14 from T2;
END;
Here the rest of the fourteen columns of T3 have the relation first column id.
Whenever I call the above stored procedure all the records in into T3 is inserting with top 1 id in table T1 time even though I have total 10 id's in T1.
After close observation I came to know that the reason for that is as I'm mentioning the limit 1 so it is fetch only first id every time.
If I didn't mention limit 1 it is returning all the 10 id's and the query it self is failing.
Is there anyway I can get all id's in table T3. Please suggest me.
Thanks in Advance.
To elaborate or the comment by #jarlh (who is absolutely correct) you need to have a join between the two tables rather than selecting a value in a subquery
INSERT INTO T3
(id
,col1
,col2
,...
,col14)
SELECT T1.id
,T2.col1
,T2.col2
,...
FROM T1
INNER JOIN T2 ON
(T1.somecol1=T2.somecol1,
T1. somecol2= T2.somecol2,
T1.somecol3 = T2.somecol3,
T1.somecol4= T2.somecol4)
With your code as it is, you would only ever expect a single value from T1.