I have many categories with the same name and parent in my Opencart database (duplicates). Need to find all of them. That's my query:
SELECT *
FROM
(SELECT `oc_category`.category_id,
`oc_category`.parent_id,
`oc_category_description`.name
FROM `oc_category`, `oc_category_description`
WHERE `oc_category`.category_id = `oc_category_description`.category_id
) cats
GROUP BY `cats`.parent_id, `cats`.name
HAVING COUNT(*) > 1
But this query returns nothing. Please tell me if I'm wrong.
No problem with the query, it does work, check this out:
http://sqlfiddle.com/#!9/3d170/4
Please fiddle with that and populate it with the data which produces no records, and add it to your question.
Related
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?
I have this query that creates a list of 10 names that are in my facebookfriends list that are not already in my contact list, but I want it to randomly select these 10 names.
I can't get the other examples to work with my query, please can you help me get this right.
Here is the sql:
SELECT TOP 10 FaceBookFriends.FaceBookName, FaceBookFriends.FullName,
FaceBookFriends.FirstName, FaceBookFriends.Surname,
FaceBookFriends.DateAdded
FROM FaceBookFriends LEFT JOIN Contact_List
ON FaceBookFriends.[FullName] = Contact_List.[Full _Name]
WHERE (((Contact_List.[Full _Name]) Is Null));
I added a ORDER BY Rnd(FaceBookFriends.FullName) at the end of the report but it doesn't work.
Try this in order by clause:
order by newid()
Can anyone help me with a problem I am having with a CrossTab Query to compare current prices from our suppliers?
The select Query that it works from has a sub query that selects on only the most resent prices for our price comparison and this works perfectly for the data we need, see below:
qryPriceComp:
SELECT tblPriceComp.SupplyerID, tblPriceComp.ProductID,
tblPriceComp.Effdt, tblPriceComp.CostPrice,
tblProduct.Product, tblSupplier.Supplier
FROM tblSupplier INNER JOIN
(tblProduct INNER JOIN tblPriceComp ON tblProduct.ProductID = tblPriceComp.ProductID)
ON tblSupplier.SupplierID = tblPriceComp.SupplyerID
WHERE (((tblPriceComp.Effdt) In
(SELECT MAX(B.EffDt) AS MaxOfDt FROM tblPriceComp AS B
WHERE tblPriceComp.ProductID=B.ProductID
AND tblPriceComp.SupplyerID=B.SupplyerID
AND B.EffDt <= Date()+1)));
This is then used for the crosstab query
qryPriceComp_Crosstab:
TRANSFORM Sum(qryPriceComp.CostPrice) AS SumOfCostPrice
SELECT qryPriceComp.Product
FROM qryPriceComp
GROUP BY qryPriceComp.Product
ORDER BY qryPriceComp.Product, qryPriceComp.Supplier
PIVOT qryPriceComp.Supplier;
But when run it gives an error that both tblPriceComp.ProductID and tblSupplier.SupplierID are invalid. I have tried adding them as perimeters but when run this gives a box to enter the ID numbers which is no good as we want to see all productIDs and SupplyerIDs. If anyone can help it would be greatly appreciated!
Not a real solution, but a usable workaround:
Change qryPriceComp to a INSERT INTO tempTable query, and then base the crosstab query on tempTable.
Before each INSERT run, a DELETE * FROM tempTable must be executed.
I have 2 paramters ( $memberparamter and $rest_id), that i am getting from the user. But every time my server runs the statement, it does not find anything. I have double checked with my database, and it says the desired output, does exist. If i delete one of the where clause, it works great.
Have i done it the wrong way?
This is my sql statement:
SELECT
eso.order_id as order_id,
eso.member_id as member_id,
esoi.title as title,
dl.used_date as checked,
dl.order_item_id as order_item_id
FROM exp_store_orders as eso
inner join exp_store_order_items as esoi on (eso.order_id = esoi.order_id)
inner join exp_deal_keys as dl on dl.order_item_id = esoi.order_item_id
where eso.member_id = '$memberparamter' and esoi.entry_id = '$rest_id'
and eso.order_paid > 0
group BY eso.transaction_id
ORDER BY eso.transaction_id desc
You need to specify which where clause fixes the problem. If I were to speculate, I would guess that you misspelled '$memberparamter' and it really should be '$memberparameter' -- on the belief that you would spell "parameter" correctly in your code.
Is it the GROUP BY that is causing the problem. Why do you have GROUP BY when you are not aggregating any of your SELECT columns?
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?