I need to find all rows in my table where the strings of a specific field are duplicates in two or more places.
Can that be done in a MySQL statment?
EDIT
I need to get every row not just a count of how many duplicates there are. I want to be able to edit the fields.
Yes, try something like this:
SELECT *
FROM `YourTable`
WHERE `YourColumn` IN (
SELECT `YourColumn`
FROM `YourTable`
GROUP BY `YourColumn`
HAVING COUNT(*) > 1
)
Yes, using GROUP BY and HAVING.
SELECT mycolumn, count(*) FROM mytable
group by mycolumn
having count(*) > 1
Related
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
I'm going to count returned rows by this query:
select * from tableName where( number > 1000);
I tried this query to get rows count:
select count(*) as count from (select * from tableName where( number > 1000));
but I got syntax error. What is wrong with that?
You dont want Nested Query Just Use
select count(*) as count from tableName where number > 1000 ;
This works if you are using nested query & don't use 'count' as your temporary variable name:
select count(temp.id) as num from (select * from tableName where number > 1000) temp
It should be like this
select count(*) as noOfCount from tableName where number > 1000;
Do not use sql reserved keywords as your temp variable names
Why not counting the rows in 1 select directly:
select count(*) from tableName where number>1000;
Make it simple. Don't need a subquery
select count(*) as count from tableName where number > 1000;
Likely, the syntax error your are getting is "every derived table must have an alias".
To fix that syntax error, you would just assign an alias to the inline view query.
SELECT foo FROM (SELECT foo FROM bar) a ;
^
But for your specific query, an inline view isn't required.
You could simply modify your original query, to replace the * in the SELECT list with an aggregate expression such as COUNT(*). You can also assign an alias to the aggregate expression.
It's valid to use COUNT as a column alias, but my preference would be to use a different alias, one that isn't the name of a MySQL function.
FIrst thing first
SELECT COUNT(*) as tot_rows FROM `tableName` WHERE `number` > 1000 ;
you better give back tilt (`) sign arond number, cause might be there's a keyword in mysql called number. I am not pretty sure, but you should take precaution.
Second you can do another thing also.
If you are using PHP then
$query_result = mysql_query("SELECT * FROM `tableName` WHERE `number` > 1000");
$num_row = mysql_numrows($query_result);
This query works fine
select count(*) as count from tableName where number > 1000 ;
but FYR in ur query error is u don't define subquery name in example its tbl
select count(*) as count from (select * from tableName where( number > 1000)) as tbl;
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()
I obtain a series of values that appear only one time in my database using COUNT in mysql that list below:
valueName
---------
value1
value2
value3
value4
I need a script that retrieves all records in a table where valueName are not the values listed in the initial count, and I need this two steps to run in a single script (doesn't matter how many parts it has).
I've got the script to obtain the list above like this:
SELECT field AS new_name FROM table GROUP BY field HAVING COUNT(field) = 1;
And it works.
The problem is that I don't know how to work with the aggregated result of the first step. Maybe using some kind of function. Or loop (I don't think in SQL..).
I've tried different things like attaching a COUNT inside a WHERE clause and others but it doesn't work.
Please help!
Use a join:
select t.*
from table t join
(SELECT field
FROM table
GROUP BY field
HAVING COUNT(field) > 1
) filter
on t.field = filter.field;
If you have a primary key in your table and an index on table(field, pk), the following is probably faster:
select t.*
from table t
where exists (select 1
from table t2
where t2.field = t.field and t2.pk <> t.pk
);
Try this:
SELECT table.* FROM table
JOIN
(SELECT field FROM table GROUP BY field HAVING COUNT(field) > 1) newtable
ON
table.field = newtable.field;
This should work.
I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
1 example#hotmail.com
2 example12#hotmail.com
...and skip the duplicate values.
SELECT MIN(id), email FROM some_table GROUP BY email
SELECT DISTINCT email FROM table
If you don't need ID use
SELECT DISTINCT email FROM `TABLE_NAME`
else If you need the First ID use
SELECT MIN(ID),email FROM `TABLE_NAME` GROUP BY email
There are several ways to accomplish this, one is to use the DISTINCT clause:
SELECT DISTINCT email FROM your_table;
another way is to summarize counts of the values:
SELECT COUNT (*), email from your_table GROUP BY email;
SELECT DISTINCT UNIQUE_FEILD_NAME FROM YOUR_TABLE_NAME