I need one help.I need query to skip duplicate column value according to the auto increment id using MySQL.I am explaining my table structure below.
db_subcat:
member_id day_id
16 2
18 2
18 2
18 3
Here i need to skip the duplicate column value according to the member_id e.g- here for member_id-18 there are two day_id as 2,i need to fetch only one if member_id is same.
you can use distinct:
select distinct member_id, day_id from db_subcat;
you can use distinct as well as group by
select distinct member_id, day_id from db_subcat;
select member_id, day_id from db_subcat group by member_id, day_id;
Here distinct will be faster than group by. To see the difference have a look at http://charlesnagy.info/it/mysql/difference-between-distinct-and-group-by
You can use the UNIQUE key in MySQL so that the duplicate results will not be inserted in the first place.
Else, you can use SELECT distinct:
SELECT distinct member_id, day_id from db_subcat;
More information on SQL Unique Constraint: http://www.w3schools.com/sql/sql_unique.asp.
More information on SQL Select Distinct: http://www.w3schools.com/sql/sql_distinct.asp.
Apart from skipping the duplicate member id, you may want the sum of day_id as well, below query fetches that:
select member_id, sum(day_id)
from db_subcat
group by member_id
You can use any aggregate function (e.g. min, max, count, avg) according to the requirement.
Related
I would like to know how can i make this code: if is not empty insert into table, else overwrite the rows:
$affectedRows = $pdo->exec(
"insert into table_1 (name, money, month, year)
select name, count(*), MONTH(STR_TO_DATE(data, '%d/%m/%Y')),
YEAR(STR_TO_DATE(data, '%d/%m/%Y'))
from table_2
group by name");
Thanks
Use the following query:
INSERT INTO table_1 (name, money, month, year)
SELECT t.name, t.money, t.month, t.year
FROM
(
SELECT name,
COUNT(*) AS money
MAX(MONTH(STR_TO_DATE(data, '%d/%m/%Y'))) AS month,
MAX(YEAR(STR_TO_DATE(data, '%d/%m/%Y'))) AS year
FROM table_2
GROUP BY name
) t
ON DUPLICATE KEY UPDATE name=t.name, money=t.money, month=t.month, year=t.year
Note that I wrapped your calls to MONTH() and YEAR() with MAX(), an aggregate function, because it is incorrect to select a non aggregate column when using GROUP BY. If you show us actual data for table_2, then perhaps this could be improved upon.
Update:
Note that the subquery whose result set is used for insertion above can never generate duplicate records, because there can only ever be a single record per group. So to test this answer, you would already have to have some data in table_1 which would be duplicated by the subquery.
I have the following database and want to delete the red ones because they are doubouled. So I have to check every row if another row is matching by pid, price, price_old, link and shop.
But how can I check that and how can I delete it then?
Maybe an easier way would be to generate a id from the values inside each row. So if the values inside a row would be equal also the id would be equal and who have only one value to compare with the other id's.
Is that a better way? - If yes, how can I do that?
Greetings!
Do the fact you have no way for get thi distinct row you could add uniqie id using
ALTER TABLE my_table
ADD id int NOT NULL AUTO_INCREMENT
Once done you could use not in where the id are not the min grouped by the value you need for define the duplication
delete from my_table
where id NOT in ( select min(id) from my_table
group by shop, link
)
The simplest way is to run a distinct query:
select distinct pid, price, price_old, link, shop
from t;
You can create a new table using into. That is the simplest way. Because all columns are the same, MySQL doesn't offer a simple method to delete duplicate rows (while leaving one of them).
However, it is possible that your current results are generated by a query. If so, you can just add select distinct to the query. However, it would be better to fix the query so it doesn't generate duplicates. If this is the case, then ask another question with sample data, desired results (as text, not an image), and the query you are currently using.
Test this first on a test table:
DELETE t1
FROM t t1, t t2
WHERE t1.id > t2.id AND t1.price = t2.price
AND t1.link = t2.link AND t1.shop = t2.shop
AND t1.price_old = t2.price_old;
Basically you are removing the one with the highest ID if those parameters are equal
select * from
(select pid, price, price_old, link ,
row_number() over(partition by pid, price, price_old, link, shop order by pid) as rank
from my_table) temp
where temp.rank = 1
This Query will group by all the columns first and rank them. Duplicate rows will have rank > 1. It does not matter we take first or second row as both are copy of each other. We just take rows with rank 1. Rows that are not duplicate will also be having rank 1 and hence won't be neglected.
One more way to this is by using union.
select * from my_table UNION select * from my_table
I have a table called story_comment with (integer) columns story_id, and comment_id. I want to know how many comments each story has but I'm not sure the best way to write the sql query.
The query should return a table with the columns story_id and num_comments (where num_comments is the number of rows in story_comment where story_id is the same as the story_id in that results row).
Desired Results (Example):
story_id | num_comments
4 | 17
6 | 0
7 | 4
I was able to do this for one particular story_id with the query:
SELECT story_id, Count(story_id) as num_comments FROM story_comment where story_id=20;
but I'm not sure how I can do this for every story_id in the table. Side note I'm going to be doing this query using mysqli in php.
Use GROUP BY
SELECT story_id, Count(story_id) as num_comments FROM story_comment
GROUP BY story_id
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
To make aggregate functions like count() apply to each unique value of a column instead to the complete table, add a group by
SELECT story_id, Count(*) as num_comments
FROM story_comment
group by story_id
I am using MySQL and PHP.
I tried to parameterize the queries using bind_param. Now I need to pass a list of integers to the parameterised IN Query.
I have tried with the below query:
select id,date,
sum(value) as distance ,
(select number from table2 where id=id) as Number
from Table1 where id in (1777,1778,1779)
But it is returning only the result for the first value in the list (1777).
How can I get the result for all the entries (1777,1778,1779) in the list?
You need to use GROUP BY e.g.
SELECT
id,
date,
sum(value) as distance ,
(SELECT number FROM table2 WHERE id=id) as Number
FROM Table1
WHERE
id in (1777,1778,1779)
GROUP BY id
Hope this helps!
Below is the table I have created, my goal is to get the sum of the value depending of each person has, and exclude any username duplicate.
username | value
Bob 5
Vicky 10
Bob 12
Desire results:
username | value
Bob 17
Vicky 10
This is what the GROUP BY clause do, use GROUP BY username with SUM(value):
SELECT username, SUM(value)
FROM tablename
GROUP BY username;
SQL Fiddle Demo
When you add a GROUP BY clause, rows that have the same values in the list of columns you specify in it, will be gathered into a group of unique values. For the other columns that are not listed in the GROUP BY clause, the aggregate function will be applied to it, the SUM(value) in the query above.
Try this
SELECT T.username, SUM(T.value) AS value
FROM your_table_name T
WHERE T.username IS NOT NULL
GROUP BY T.username
Try This one
SELECT username, SUM(value) as val FROM tablename GROUP BY username;
Use the following query:
SELECT url, SUM(pos) FROM import_images GROUP BY url