MySQL find duplicates based on some matching fields and some not - mysql

I am trying to create a MySQL query of game discs to find some duplicates based on some values being the same and some not.
I need the fields 'name', 'disc', 'platform', 'region' to be the same.
But I also need the field 'version' to be different.
I have already tried a number of queries to do this such as the one below but none seem to work as desired.
SELECT *
FROM media.amps_2000_box_a
INNER JOIN (SELECT *
FROM media.amps_2000_box_a
GROUP BY name
HAVING COUNT(name) > 1) dup
ON media.amps_2000_box_a.name = dup.name and media.amps_2000_box_a.disk = dup.disk and media.amps_2000_box_a.format = dup.format and media.amps_2000_box_a.region = dup.region and media.amps_2000_box_a.version<> dup.version
order by dup.name;
Would anyone be able to help me fix this query?
Thanks in advance.
slick

Maybe I'm missing something but the solution seems quite trivial:
SELECT DISTINCT
amps1.name,
amps1.disk,
amps1.platform,
amps1.region
FROM media.amps_2000_box_a amps1
JOIN media.amps_2000_box_a amps2
ON amps1.name = amps2.name
AND amps1.disk = amps2.disk
AND amps1.platform = amps2.platform
AND amps1.region = amps2.region
AND amps1.version <> amps2.version
ORDER BY amps1.name;

Related

Error Code 1242: Subquery retuens more than 1 row

I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?

Wrong syntax in a SQL query

I'm trying to write a query that takes a point, given to a player in a match event, multiply it by another column and then by 3, and then copy the result in the players table where the player already exists.
There is an error but I can't find what's wrong. Can you help me, please, with solving this issue?
UPDATE
j7yh8_bl_match_events,
j7yh8_bl_players
SET
j7yh8_bl_players.player_points = SELECT COUNT(j7yh8_bl_match_events.e_id) * j7yh8_bl_match_events.ecount * 3,
WHERE
j7yh8_bl_match_events.player_id = j7yh8_bl_players.id AND j7yh8_bl_match_events.e_id = 5;
Any help will be appreciated.
Thank you.
In MySQL, you can use a join with update. Any aggregation functions, though, have to be in subqueries.
I'm not 100% sure that the expression is for calculating points. It seems that something like this solves your problem:
UPDATE j7yh8_bl_players p LEFT JOIN
(SELECT me.player_id, SUM(me.ecount) * 3 as points
FROM j7yh8_bl_match_events me
WHERE me.e_id = 5
GROUP BY me.player_id
) me
ON me.player_id = p.id
SET p.player_points = me.points

MySQL Filtering Attributes

I am trying to create a better way of filtering items on a website. The actual filtering is working great but displaying the available options has stumped me.
I am using this sql query to get all the options for a specific category.
SELECT
atr1.`type` , atr1.`value`
FROM
`index-shop-filters` AS atr1
JOIN
`index-shop-filters` as atr2 ON atr1.`item` = atr2.`item`
WHERE
( atr2.`type` = 'sys-category' AND atr2.`value` = '1828' )
GROUP BY
atr1.`type`, atr1.`value`
But when I add a selected filter in the hope to get the remaining filters available. It doesn't give me the remaining filters. Instead it ignores the second OR statement.
SELECT
atr1.`type` , atr1.`value`
FROM
`index-shop-filters` AS atr1
JOIN
`index-shop-filters` as atr2 ON atr1.`item` = atr2.`item`
WHERE
( atr2.`type` = 'sys-category' AND atr2.`value` = '1828' )
OR ( atr2.`type` = 'Manufacturer' AND atr2.`value` = 'Sony' )
GROUP BY
atr1.`type`, atr1.`value`
I tried adding the HAVING COUNT(*) = 2 but that doesn't get the correct results.
The data in the index-shop-filters is like this.
item,type,value
the types are sys-category, manufacturer, size, color, etc.
When they select the first option (like sys-category) it will then display the available options. If they then select manufacturer (like sony) it will then display the available options that the items are sony, and in the category.
Ok, I think I finally understand what you're trying to do: you aren't trying to get a list of items, you're trying to get a list of item filters. Sorry, I should have picked up on that sooner.
Anyway, now I understand the problem, but I don't have a great answer. What you're trying to do is fairly complicated, and it can't be done with just one join (as far as I know). I can only think of two ways to do this: with multiple subqueries or with multiple joins. Both of these solutions are complicated and do not scale well, but it's all I can think of.
Here is one possible solution, using subqueries, that I do not recommend:
SELECT item, `type`, `value`
FROM `index-shop-filters` AS f
WHERE f.item IN (SELECT item FROM `index-shop-filters` WHERE `type` = 'sys-category' AND `value` = '1828')
AND f.item IN (SELECT item FROM `index-shop-filters` WHERE `type` = 'Manufacturer' AND `value` = 'Sony')
Here is a solution, using joins, that is better but still not great:
SELECT item, `type`, `value`
FROM `index-shop-filters` AS f
JOIN `index-shop-filters` AS f2 ON f.item = f2.item AND f2.`type` = 'sys-category' AND f2.`value` = '1828'
JOIN `index-shop-filters` AS f3 ON f.item = f3.item AND f3.`type` = 'Manufacturer' AND f3.`value` = 'Sony'
And that's all I've got. Both of those solutions should work, but they won't perform well. Hopefully someone else can come up with a clever, scalable answer.

MySQL COUNT() causing empty array() return

MySQL Server Version: Server version: 4.1.14
MySQL client version: 3.23.49
Tables under discussion: ads_list and ads_cate.
Table Relationship: ads_cate has many ads_list.
Keyed by: ads_cate.id = ads_list.Category.
I am not sure what is going on here, but I am trying to use COUNT() in a simple agreggate query, and I get blank output.
Here is a simple example, this returns expected results:
$queryCats = "SELECT id, cateName FROM ads_cate ORDER BY cateName";
But if I modify it to add the COUNT() and the other query data I get no array return w/ print_r() (no results)?
$queryCats = "SELECT ads_cate.cateName, ads_list.COUNT(ads_cate.id),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName ORDER BY cateName";
Ultimately, I am trying to get a count of ad_list items in each category.
Is there a MySQL version conflict on what I am trying to do here?
NOTE: I spent some time breaking this down, item by item and the COUNT() seems to cause the array() to disappear. And the the JOIN seemed to do the same thing... It does not help I am developing this on a Yahoo server with no access to the php or mysql error settings.
I think your COUNT syntax is wrong. It should be:
COUNT(ads_cate.id)
or
COUNT(ads_list.id)
depending on what you are counting.
Count is an aggregate. means ever return result set at least one
here you be try count ads_list.id not null but that wrong. how say Myke Count(ads_cate.id) or Count(ads_list.id) is better approach
you have inner join ads_cate.id = ads_list.category so Count(ads_cate.id) or COUNT(ads_list.id) is not necessary just count(*)
now if you dont want null add having
only match
SELECT ads_cate.cateName, COUNT(*),
FROM ads_cate INNER JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
having not count(*) is null
ORDER BY cateName
all
SELECT ads_cate.cateName, IFNULL(COUNT(*),0),
FROM ads_cate LEFT JOIN ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName
Did you try:
$queryCats = "SELECT ads_cate.cateName, COUNT(ads_cate.id)
FROM ads_cate
JOIN ads_list ON ads_cate.id = ads_list.category
GROUP BY ads_cate.cateName";
I am guessing that you need the category to be in the list, in that case the query here should work. Try it without the ORDER BY first.
You were probably getting errors. Check your server logs.
Also, see what happens when you try this:
SELECT COUNT(*), category
FROM ads_list
GROUP BY category
Your array is empty or disappear because your query has errors:
there should be no comma before the FROM
the "ads_list." prefix before COUNT is incorrect
Please try running that query directly in MySQL and you'll see the errors. Or try echoing the output using mysql_error().
Now, some other points related to your query:
there is no need to do ORDER BY because GROUP BY by default sorts on the grouped column
you are doing a count on the wrong column that will always give you 1
Perhaps you are trying to retrieve the count of ads_list per ads_cate? This might be your query then:
SELECT `ads_cate`.`cateName`, COUNT(`ads_list`.`category`) `cnt_ads_list`
FROM `ads_cate`
INNER JOIN `ads_list` ON `ads_cate`.`id` = `ads_list`.`category`
GROUP BY `cateName`;
Hope it helps?

Nested MySQL query?

I have this MySQL statement which works fine:
SELECT claim_items.item_id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
However what I want to do is to get the sum of the per_item field generated in the MySQL statement. Is it possible to do a nested statement to do that? I know I could create a view and then do it, but would prefer to do it in one statement.
Thanks for your help, much appreciated!
select t.id, SUM(t.per_item)
from
(
SELECT claim_items.item_id as id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
) t
group by t.id
Hope this should help.
If you just want the sum over all rows you can do it like that (using explicit joins for better readability):
SELECT SUM( claim_items.quantity * items_rate.rate )
FROM claim_items
JOIN items_rate ON ( items_rate.items_id = claim_items.item_id )
WHERE claim_items.claim_id = 1