delete duplicate by considering specific criteria - duplicates

I would like to ask you to remove duplicates but remain the specific one.
if there is a "FD" in the type the code should not deleted it.
I would be very apprecated for any help. I stucked in :(
Thank you

the easiest way to do this, is to have a unique identifier on every row. so if you don't already have one, create a new column called "row_id" and set it to AutoNumber. save the table, you should now have a unique id on every row.
Next, you can construct a big ugly sql to do what you need, but if your table is large, it won't perform well with sub-queries. so its probably best to use a temporary table to save the duplicate rows, and then reference it for your delete statement.
so first identify the duplicates, and store the min row id.
SELECT a.SerWorkNumber, a.SerialNo, a.Type, Min(a.row_id) AS MinOfrow_id INTO CD_FD_Dups
FROM CD_FD AS a
GROUP BY a.SerWorkNumber, a.SerialNo, a.Type
HAVING count(1) > 1;
next, delete from the actual table referencing the temp table you created above:
DELETE
FROM CD_FD AS a
WHERE EXISTS (
SELECT 1
FROM CD_FD_Dups d
WHERE d.SerWorkNumber = a.SerWorkNumber
and d.SerialNo = a.SerialNo
and d.Type = a.Type
and a.row_id > d.MinOfrow_id
);

Related

Append records from one table to another using the common field

There are many varied posts about this matter, but I am unable to find the answer I need. I am hoping this question is unique.
I am trying to append all the data from one table to another, without creating new records. The data in the second table is really a subset of data for a portion of the existing records in the first table.
For example:
I have the table "SPK". And I want to write all of the data from SPK into the table "RCT". The common field between each record I want to match is the RegID, which is unique in both tables (i.e. there is only one SPK record per RCT record).
If I understand correctly, you mean append the columns in one table (call it SECOND) to the other (call it FIRST).
In that case, does this work ?
UPDATE
regcontactsTest
JOIN
speakersTest
ON speakersTest.RegistrationID = regcontactsTest.RegistrationID
SET regcontactsTest.presentationtitle = speakersTest.presentationtitle
EDIT: Updated the query based on Mariadb syntax
You need to use JOIN. For general Update join :
update tab1 a
join tab2 b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
Or in other words:
update
tab1 a
join tab2 b on ..
set a.field=...;

Create a new table from multiple tables with identical column names without declaring the column names

I have 3 tables that I'd like to pull data from and use the result set to create a new table. Note that each of these tables have identical column names.
CREATE TABLE smsout_32020Nov2014
AS
(SELECT *
FROM smsout17nov2014b,smsoutnov32014,smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020);
Problem is that I am getting an error that there are duplicate column names
Is there a work around?
As described in comments on your question, it is unclear what results you are actually trying to obtain. Supposing that the question reflects a complete misunderstanding of join operations, however, it may be that UNION ALL is what you're actually looking for. In particular, if you want all the rows of the three named tables for which the shortcode column has the value 32020, then that would be this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT smsout17nov2014b.*
WHERE shortcode = 32030
UNION ALL
SELECT smsoutnov32014.*
WHERE shortcode = 32030
UNION ALL
SELECT smsout.*
WHERE shortcode = 32030
)
On the other hand, if the results you are selecting are in fact in the form you want, as you have revised your question to say, then you have no alternative but to assign explicit column names to ensure column name uniqueness (so at least for every column in two of the three base tables). You can do this via aliases in the SELECT statement or via a column list in the outer CREATE TABLE statement (or both). Your original question seemed to say that the workaround you wanted was a way to avoid doing that, but now it just seems to say that you want to fix the error.
It will be a bit simpler to do the patch up in the SELECT statement. Based on your revised starting query, that would be something like this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT
smsout.*,
m.col1 AS month_col1, m.col2 AS month_col2, ... m.shortcode AS month_shortcode,
c.col1 AS code_col1, c.col2 AS code_col2, ... c.shortcode AS code_shortcode,
FROM
smsout17nov2014b m,
smsoutnov32014 c,
smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020
);
The columns of your new table will be col1, col2, ... shortcode, month_col1, month_col2, ... month_shortcode, code_col1, code_col2, ... code_shortcode.
Note, by the way, that even if those results are in the form you want (which I find surprising), I have trouble believing that the rows are what you want.

select from database table rows that are not in another table

I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)

Find matches from 2 tables, change other field?

I have a database with two separate tables. One table (T1) has 400+ values in its only column, while the other (T2) has 14,000+ rows and multiple columns.
What I need to do is to compare the column in T1 to one column in T2. For every matching value, I need to update a different value in the same row in T2.
I know this is pretty easy and straight-forward, but I'm new to MySQL and trying to get this down before I go back to other things. Thanks a ton in advance!
EDIT: Here's what I've been trying to no avail..
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` LIKE `Source`.`SourceID`
EDIT 2:
A little clarification:
apollo.Bottom and apollo.Source are the two tables.
apollo.Bottom is the table with one column and 400 records in that column.
I want to compare Bottom.URL to Source.SourceID. If they match, I want to update Source.CaptureInterval to 12.
You can use the following query to update. But the performance will be much better if you index URL and SourceID columns in both tables as they are being used in the WHERE clause.
UPDATE `apollo`.`Source`, `apollo`.`Bottom`
SET `Source`.`CaptureInterval` = '12'
WHERE `Bottom`.`URL` = `Source`.`SourceID`
You can join the two tables together and do a multiple table update.
Start with something like this:
UPDATE `apollo`.`Source`
INNER JOIN `apollo`.`Bottom` ON `apollo`.`Bottom`.`URL` = `apollo`.`Source`.`SourceID`
SET `apollo`.`Source`.`CaptureInterval` = '12';

How can i update a table based on a count of another table while using LIKE statement

I know how to update one table's field from another table's count using t1.id=t2.id etc.. but i have somewhat typical issue. I have to use LIKE STATEMENT in WHERE clause.
This is something similar i wanted to do.
UPDATE `CATEGORIES`
SET `num_listings` = (SELECT COUNT(*)
FROM `LISTINGS`
WHERE `LISTINGS`.`CATEGORY` LIKE
ws_concat('', "%-", `CATEGORIES`.`ID`, "-%"));
(Example: I have CATEGORY stored as -25- in the LISTINGS table as a field name CATEGORY)
I understand that i cannot use ws_contact here but is there another way to achieve it?
Thanks in advance.
Unless there is a good reason for the category ID to be represented only by a part of a string in the listings table, the best way to handle such a structure of data is to add a category_id column to the LISTINGS table, and make sure that when adding or editing a listing this column is populated properly.
This would allow to simply JOIN the two tables ON categories.id = listings.category_id and makes much more sense. This would also give better performance by far.
If you do want to keep the DB structure as is, you can use a temporary table, with LIKE and CONCAT:
DROP TABLE IF EXISTS temp;
CREATE TABLE temp AS
SELECT categories.id, COUNT(*) AS c
FROM categories
JOIN listings ON listings.category LIKE CONCAT('%',categories.id,'%')
GROUP BY categories.id;
UPDATE categories, temp
SET categories.num_listings = temp.c
WHERE categories.id = temp.id;