MySQL: SELECT UNIQUE VALUE - mysql

In my table I have several duplicates. Ineed to find unique values in mysql table column.
SQL
SELECT column FROM table
WHERE column is unique
SELECT column FROM table
WHERE column = DISTINCT
I've been trying to Google, but almost all queries are more complex.
The result I's like is all non duplicate values.
EDIT
I'd like to have UNIQUE values...
Not all values one time... (Distinct)

Try to use DISTINCT like this:
SELECT DISTINCT mycolumn FROM mytable
EDIT:
Try
select mycolumn, count(mycolumn) c from mytable
group by mycolumn having c = 1

Here is the query that you want!
SELECT column FROM table GROUP BY column HAVING COUNT(column) = 1
This query took 00.34 seconds on a data set of 1 Million rows.
Here is a query for you though, in the future if you DO want duplicates, but NOT non-duplicates...
SELECT column, COUNT(column) FROM table GROUP BY column HAVING COUNT(column) > 1
This query took 00.59 seconds on a data set of 1 Million rows. This query will give you the (column) value for every duplicate, and also the COUNT(column) result for how many duplicates. You can obviously choose not to select COUNT(column) if you don't care how many there are.
You can also check this out, if you need access to more than just the column with possible duplicates... Finding duplicate values in a SQL table

Try this one:
SELECT COUNT(column_name) AS `counter`, column_name
FROM tablename
GROUP BY column_name
WHERE COUNT(column_name) = 1
Have a look at this fiddle: http://sqlfiddle.com/#!9/15147/2/0

Try this:
SELECT DISTINCT (column_name) FROM table_name

Related

SQL query - union on NOW()

I tried making a SQL query and union the result on the current time, but I cannot seem to find a neat way to solve this.
I've tried the following:
SELECT * FROM `accounts`
UNION SELECT NOW()
And Sequel Pro just reports The used SELECT statements have a different number of columns.
The accountstable just has three columns:
ID (INT(32), AUTO_INC)
CREATED (Timestamp)
NAME (VAR_CHAR(28))
I anticipated I'd get a response with four columns: ID, CREATED, NAME, NOW
What do I do wrong?
Union means that the records from the second query will be appended to those retrieved from the first one.
So the two tables must have the same structure for this to work.
For example:
SELECT field1,field2,field3 FROM tableA
UNION
SELECT field1,field2,field3 FROM tableB
What you want to do is
SELECT *, NOW() as now FROM `accounts`
This will retrieve all the records from the accounts table and will add the timestamp to all the rows on a column named "now" (this is just an alias so use whatever you like).
try this
SELECT *,now() as now FROM `accounts`

which one is faster and why

select id, name from myTable;
or
select id, name from myTable where name like "%";
Does Mysql optimize the second query?
Update: myTable has only three columns id(primary key), name and age .No of rows in myTable are in range(5-10)k.
what if we create an index on column "name"?
I think the execution of second query will be faster.
In the order of execution of queries WHERE clause comes before select. So if you are using Where then it will filter the no. of records. When select will work on top of it , Select will work faster(specially when you have huge data in DB).
Reference:
What is the order of execution for this SQL statement

Find and remove duplicate rows by two columns

I read all the relevant duplicated questions/answers and I found this to be the most relevant answer:
INSERT IGNORE INTO temp(MAILING_ID,REPORT_ID)
SELECT DISTINCT MAILING_ID,REPORT_IDFROM table_1
;
The problem is that I want to remove duplicates by col1 and col2, but also want to include to the insert all the other fields of table_1.
I tried to add all the relevant columns this way:
INSERT IGNORE INTO temp(M_ID,MAILING_ID,REPORT_ID,
MAILING_NAME,VISIBILITY,EXPORTED) SELECT DISTINCT
M_ID,MAILING_ID,REPORT_ID,MAILING_NAME,VISIBILITY,
EXPORTED FROM table_1
;
M_ID(int,primary),MAILING_ID(int),REPORT_ID(int),
MAILING_NAME(varchar),VISIBILITY(varchar),EXPORTED(int)
But it inserted all rows into temp (including duplicates)
The best way to delete duplicate rows by multiple columns is the simplest one:
Add an UNIQUE index:
ALTER IGNORE TABLE your_table ADD UNIQUE (field1,field2,field3);
The IGNORE above makes sure that only the first found row is kept, the rest discarded.
(You can then drop that index if you need future duplicates and/or know they won't happen again).
This works perfectly in any version of MySQL including 5.7+. It also handles the error You can't specify target table 'my_table' for update in FROM clause by using a double-nested subquery. It only deletes ONE duplicate row (the later one) so if you have 3 or more duplicates, you can run the query multiple times. It never deletes unique rows.
DELETE FROM my_table
WHERE id IN (
SELECT calc_id FROM (
SELECT MAX(id) AS calc_id
FROM my_table
GROUP BY identField1, identField2
HAVING COUNT(id) > 1
) temp
)
I needed this query because I wanted to add a UNIQUE index on two columns but there were some duplicate rows that I needed to discard first.
For Mysql:
DELETE t1 FROM yourtable t1
INNER JOIN yourtable t2 WHERE t1.id < t2.id
AND t1.identField1 = t2.identField1
AND t1.identField2 = t2.identField2;
You will first need to find your duplicates by grouping on the two fields with a having clause.
Select identField1, identField2, count(*) FROM yourTable
GROUP BY identField1, identField2
HAVING count(*) >1
If this returns what you want, you can then use it as a subquery and
DELETE FROM yourTable WHERE field in (Select identField1, identField2, count(*) FROM yourTable
GROUP BY identField1, identField2
HAVING count(*) >1 )
you can always get the primary ids by grouping that two unique fields
select count(*), id as count from table group by col a, col b having count(*)>1;
and then
delete from table where id in ( select count(*), id as count from table group by col a, col b having count(*)>1) limit maxlimit;
you can also use max() in place of limit
NOTE: This solution is an alternative & old school solution.
If you couldn't achieve what you wanted, then you can try my "oldschool" method:
First, run this query to get the duplicate records:
select column1,
column2,
count(*)
from table
group by column1,
column2
having count(*) > 1
order by count(*) desc
After that, select those results and paste them into the notepad++:
Now by using the find and replace specialty of the notepad++ replace them with; first "delete" then "insert" queries like this (from now on, for security reasons, my values will be AAAA).
Special Note: Please make another new line for the end of the last line of your data inside notepad++ because regex matched the '\r\n' at the end of the each line:
Find what regex: \D*(\d+)\D*(\d+)\D*\r\n
Replace with string: delete from table where column1 = $1 and column2 = $2; insert into table set column1 = $1, column2 = $2;\r\n
Now finally, paste those queries to your MySQL Workbench's query console and execute. You will see only one occurrences of each duplicate record.
This answer is for a relation table constructed of just two columns without ID. I think you can apply it to your situation.
In a large data set if you are selecting the multiple columns in the select clause ex:
select x,y,z from table1.
And the requirement is to remove duplicate based on two columns:from above example let y,z
then you may use below instead of using combo of "group by" and "sub query", which is bad in performance:
select x,y,z
from (
select x,y,z , row_number() over (partition by y,z) as index_num
from table1) main
where main.index_num=1

Concat two columns and columns are obtained via SELECT queries

I need help with CONCAT function. I have two select queries and the result of every query is one column. I need to merge this two columns in one. Is that possible? Beacuse, I can't get result even if I try with simple select queries like:
SELECT owner FROM table WHERE number="value1";
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
These queries work and throw 3 rows like result. But, if I want to merge them in one column using CONCAT - that doesn't work. Do you know why?
SELECT CONCAT(SELECT owner FROM table WHERE number="value1",
SELECT number FROM table WHERE owner="value2" AND number IS NOT null
) as NEW_COLUMN FROM table;
I think you want this:
SELECT CONCAT(owner, number) newCol1
FROM yourTable
WHERE number="value1"
OR (owner="value2" AND number IS NOT null)
SELECT
CONCAT(owner, number) as NEW_COLUMN
FROM
table
WHERE
owner = "value2"
AND number = "value1"
AND number IS NOT NULL
The fundamental reason is that the DB cannot concatenate two different SELECTs which might have a different number of rows.
What you need to do is to re-formulate your query in terms of a JOIN.
For example suppose we have this table:
owner number
John value1
value2 123456
Your first query:
SELECT owner FROM table WHERE number="value1";
will return "John". The second one
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
will return "123456".
If you CONCAT the two values you would therefore get "John 123456".
First of all, is this the expected behaviour of the query you want? What happens is there is a third row with owner=Jack and number=value1, so that the first query returns TWO rows "John" and "Jack"?
One thing you could look into is the CROSS JOIN syntax.
SELECT CONCAT (table1.owner, ', ', table2.number) AS new_column
FROM ( SELECT owner FROM table WHERE number="value1" ) AS tablel1
CROSS JOIN
(SELECT number FROM table WHERE owner="value2" AND number IS NOT null ) AS table2;
Note that if the first query returns three rows and the second four rows, the combined query will return 3*4 = 12 rows.

MySQL GROUP BY Multiple Columns

I need to perform a GROUP BY on 2 columns separately...
In common terms, I'd like the query to say: GROUP BY column 1, then once this grouping has been performed, and the rows returned have been refined, go back to the top and GROUP BY column 2 to refine the rows returned again.
For instance, instead of stating:
GROUP BY column_1, column_2
I want to state (I Understand this is incorrect syntax):
GROUP BY column_1
GROUP BY column_2
If this is unclear I can include a sample query with expected returned results.
Are you trying to do something like this?
select ...
from (
select ...
from some_table
where ...
group by column1
) as dt
group by column2
That's the closest thing I can think of that matches what your question appears to be asking.
Mostly you can group by multiple columns in mysql. The query is:select * from table group by col1, col2
But you can't get answer as you want as. So you've another chance to get correct answer in mysql. That is, you've to use subqueries.select * from (select * from table group by col2) tabl group by col1