I have three tables:
Table_1:
id name
1 NULL
2 OLED
3 legion
4 project100
5 group3
6 0
7 25
Table_2:
projectID externalID projectTypeID projectDescription
0 0 5 UNALLOCATED
25 220339 1 OLED
Table_3:
typeID typeDesc
1 Playbook Aligned
2 Transactional Project
3 External Programs
4 UPI
5 Unallocated
I am trying to update Table_1. I only want to update rows with a 'name' that is a digit. I know that I can select those by doing:
SELECT `name`
FROM `Table_1`
WHERE `name` REGEXP '^[0-9]*$'
This gives me:
name
0
25
What I want to do now is to update these Table_1 entries based on Table_2 and Table_3. I need to find the row in Table_2 where Table_2.projectID = Table_1.name. Then, I need to find the row in Table_3 where Table_3.typeID = Table_2.projectTypeID. Finally, I need to update Table_1.name with Table_3.typeDesc. It's a confusing situation - unfortunately I can't do much to change the way that these tables are set up. Any help is appreciated.
UPDATE
Table_1
JOIN Table_2 on(Table_2.projectID = Table_1.name)
JOIN Table_3 on(Table_3.typeID = Table_2.projectTypeID)
SET Table_1.name = Table_3.typeDesc
WHERE Table_1.name REGEXP '^[0-9]*$';
Related
I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;
I wanted some help in regards to understanding how I can delete duplicate records from my database table. I have a table of 1 million records which has been collected over a 2 year period hence there is a number of records that need to be deleted as they have been added numerous times into the database.
The following is a query that I wrote based on the three columns that I am matching for duplicates, taking a count and I have also added a length of one of the columns as this will determine whether I delete all the records or just the duplicates.
SELECT
Ref_No,
End_Date,
Filename,
count(*) as cnt,
length(Ref_No)
FROM
master_table
GROUP BY
Ref_No,
End_Date,
Filename,
length(Ref_No)
HAVING
COUNT(*) > 1
;
This then gives me an output like the following:
Ref_No | End_Date | Filename | cnt | length(Ref_No)
05011384 | 2018-07-01 | File1 | 2 | 8
1234 | 2018-12-31 File2 | 11 | 4
1000002975625 | 2018-12-31 | File3 | 13
123456789123456789 | 2019-02-06 | File3 | 18
Now I have a list of rules to follow based on the length column and this will determine whether I leave the records as they are with the duplicates, delete the duplicates or delete all the records and this is where I am stuck.
My rules are the following:
If length is between 0 and 4 - Keep all records with duplicates
If length is between 5 and 10 - Delete Duplicates, keep 1 record
If length equals 13 - Delete Duplicates, keep 1 record
If length is 11, 12, 14-30 - Delete all records
I would really appreciate if some could advice on how I go about completing this task.
Thanks.
I have managed to create a temporary table in which I add a unique id. The only thing is that I am running the query twice with the length part changed for my requirements.
INSERT INTO UniqueIDs
(
SELECT
T1.ID
FROM
master_table T1
LEFT JOIN
master_table T2
ON
(
T1.Ref_No = T2.Ref_No
AND
T1.End_Date = T2.End_Date
AND
T1.Filename = T2.Filename
AND
T1.ID > T2.ID
)
WHERE T2.ID IS NULL
AND
LENGTH(T1.Ref_No) BETWEEN 5 AND 10
)
;
I then just run the following delete to keep the unique ids in the table and remove the rest.
DELETE FROM master_table WHERE id NOT IN (SELECT ID FROM UniqueIDs);
That's it.
I'm pretty new to MySQL and need to get data from a column where the id in another column of the same table matches the id in a second table and I'm not sure how to go about it.
I haven't tried anything yet as I'm too new to go about answering my own question, sorry.
So my first table looks like this
userid questionid score
-----------------------------
1 1 5
1 2 4
1 3 7
1 4 10
1 4 6
And my 2nd table looks like this
otherfields userid
---------------------
blah 1
blah 2 2
etc 3
you 4
get 5
the 6
idea 7
So what I need to do is select all the scores from table 1 where userid of table 1 matches the user id of table 2.
So you want to sum up the score for each user?
Then something like this could help:
SELECT t2.userid, t2.otherfields, SUM(t1.score) AS sum_score
FROM first_table t1
LEFT JOIN second_table t2 ON t1.userid = t2.userid
GROUP BY t2.userid
First, you join the two tables together based on userid which is the same over both tables. Then you GROUP all rows which belong to the same user (e.g. question 1-5 for user 1) together and finally you SUM up the scores of each row of a group.
I have two tables, table 1 and table 2, in a database.
I am trying to update table 1 using VBA code based on data in table 2.
Example:
Table 1
PartNo Price Description
--------------------------
A 100
B 200 Bad
C 300
Table 2
PartNo Price Description
--------------------------
A 700
B 200 Good
D 900 Used
After the update, table1 should be updated with those prices or descriptions that have changed where table1 PartNo = table 2 PartNo , and add any new items that were present in table 2.
Table 1
PartNo Price Description
--------------------------
A 700
B 200 Good
C 300
D 900 Used
I can't seem to get it quite right, appreciate the help.
You can do it with two statements, an update and an insert like this:
Update:
UPDATE Table1
INNER JOIN table2
ON(table1.partNo = table2.PartNo)
SET table1.price = table2.price,
table1.description = table2.description
And then an insert:
INSERT INTO table1 (PartNo,Price,Description)
SELECT PartNo,Price,Description FROM table2 t
WHERE NOT EXISTS(SELECT 1 FROM table1 s
WHERE t.PartNo = s.PartNo)
I request some help with MySQL when-then statement to fetch all the sid from the table after comparing multiple records having same sid but different cid-data values:
flag sid cid data
---- --- --- ----
1 300 1 john
1 300 2 john_email
1 300 3 77500
1 300 4 ok
1 301 1 jack
1 301 2 john_email
1 301 3 72210
1 301 4 notok
Here for each sid, I need to check if (sid=2 has data=john_email) AND (sid=4 has data=ok)
Only if both the conditions are satisfied, I return the sid. i.e. the output will be '300' only.
I am confused how to use the case-when-then and compare 'data' with 'john_email' and also compare data with 'ok' ... based on the cid values. Thanks for reading.
try
select sid
from your_table
group by sid
where (cid=2 and data='john_email')
or (cid=4 and data='ok')
having sum(cid=2)=1 and sum(data='john_email')=1
and sum(cid=4)=1 and sum(data='ok')=1
SQLFiddle example
You should join the table to itself, then you can check the condition in the two rows as if it was one row...
SELECT T1.sid
FROM MYTABLE T1
JOIN MYTABLE T2 ON T1.SID=T2.SID AND T1.CID=1 AND T2.CID=4
WHERE T1.DATA='john'
AND T2.DATA='ok'
Note that I used the CID values in the join clause, but you will have to adjust them if you want to join on different data rows...
What you can do is use a subquery and check if the value exists.
SELECT
*
FROM
table outertable
WHERE
( cid=2 AND data='john_email' )
AND
EXISTS ( SELECT sid FROM table WHERE cid = 4 AND data = 'ok' AND sid = outertable.sid )