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.
Related
I'm trying to update the total revenue for offices located in different geographies. The geographies are defined by circles and polygons which are both in the shapes.shape column.
When I run the query below, MySQL throws "R_INVALID_GROUP_FUNC_USE: Invalid use of group function"
I tried to adapt this answer, but I can't figure out the logic with the conditional join and geospatial data -- it's not as simple as adding a subquery with a WHERE clause. (Or is it?)
For context, I have about 350 geographies and 150,000 offices.
UPDATE
shapes s
LEFT JOIN offices ON (
CASE
WHEN s.type = 'circle' THEN ST_Distance_Sphere(o.coords, s.shape) < s.radius
ELSE ST_CONTAINS(s.shape, o.coords)
END
)
SET
s.totalRevenue = SUM(o.revenue);
UPDATE:
This works, but it's slow and confusing. Is there a faster/more concise way?
UPDATE
shapes s
LEFT JOIN (
SELECT
t.shape_id,
SUM(g.revenue) revenue
FROM
shapes t
LEFT JOIN offices o ON (
CASE
WHEN t.type = 'circle' THEN ST_Distance_Sphere(o.coords, t.shape) < t.radius
ELSE ST_CONTAINS(t.shape, o.coords)
END
)
GROUP BY
t.shape_id
) b ON s.shape_id = b.shape_id
SET
s.totalRevenue = b.revenue;
I think that speed can be helped by splitting into two UPDATEs:
... WHERE t.type = 'circle'
AND ST_Distance_Sphere ...
and
... WHERE t.type != 'circle'
AND ST_CONCAINS ...
And then see if the resulting SQLs can be simplified.
To further investigate the query, please isolate the subquery b and see if the bulk of the time is in doing that SELECT (as opposed to the time doing the UPDATE).
Please provide SHOW CREATE TABLE for each table and EXPLAIN for both the UPDATE(s) and the isolated SELECT(s). A number of clues might come from such.
Hopefully i can explain this well enough. I have a bit of a unique issue where the customer system we use can change a ID in the database in the background based on the products status.
What this means is when i want to report old products we don't use anymore along side active products there ID differs between the two key tables depending on there status. This means Active products in the product table match that of the stock item table with both showing as 647107376 but when the product is no long active the StockItem table will present as 647107376 but the table that holds the product information the id presents as -647107376
This is proving problematic for me when i comes to joining the tables together to get the information needed. Originally i had my query set up like this:
SELECT
Company_0.CoaCompanyName
,SopProduct_0.SopStiStockItemCode AS hbpref
,SopProduct_0.SopStiCustomerStockCode AS itemref
,SopProduct_0.SopDescription AS ldesc
,StockMovement_0.StmOriginatingEntityID AS Goodsin
FROM
SBS.PUB.StockItem StockItem_0
LEFT JOIN SBS.PUB.SopProduct SopProduct_0 ON StockItem_0.StockItemID = SopProduct_0.StockItemID
LEFT JOIN SBS.PUB.Company Company_0 ON SopProduct_0.CompanyID = Company_0.CompanyID
LEFT JOIN SBS.PUB.StockMovement StockMovement_0 ON StockItem_0.StockItemID = StockMovement_0.StockItemID
WHERE
Company_0.CoaCompanyName = ?
AND StockMovement_0.MovementTypeID = '173355'
AND StockMovement_0.StmMovementDate >= ? AND StockMovement_0.StmMovementDate <= ?
AND StockMovement_0.StmQty <> 0
AND StockMovement_0.StockTypeID ='12049886'
Unfortunately though what this means is any of the old product will not show because there is no matching id due to the SopProduct table presenting the StockItemID with a leading -
So from this i thought best to use a case when statement with a nested concat and left in it to bring through the results but this doesn't appear to work either sample of the join below:
LEFT JOIN SBS.PUB.SopProduct SopProduct_0 ON (CASE WHEN LEFT(SopProduct_0.StockItemID,1) = "-" THEN CONCAT("-",StockItem_0.StockItemID) ELSE StockItem_0.StockItemID END) = SopProduct_0.StockItemID
Can anyone else think of a way around this issue? I am working with a Progress OpenEdge ODBC.
Numbers look like numbers. If they are, you can use abs():
ON StockItem_0.StockItemID = ABS(SopProduct_0.StockItemID)
Otherwise a relatively simple method is:
ON StockItem_0.StockItemID IN (SopProduct_0.StockItemID, CONCAT('-', SopProduct_0.StockItemID))
Note that non-equality conditions often slow down JOIN operations.
Using an or in the join should work:
LEFT JOIN SBS.PUB.SopProduct SopProduct_0
ON SopProduct_0.StockItemID = StockItem_0.StockItemID
OR
SopProduct_0.StockItemID = CONCAT("-", StockItem_0.StockItemID)
You might need to cast the result of the concat to a number (if the ids are stored as numbers).
Or you could use the abs function too (assuming the ids are numbers):
LEFT JOIN SBS.PUB.SopProduct SopProduct_0
ON SopProduct_0.StockItemID = abs(StockItem_0.StockItemID)
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;
I wonder if anyone can spot the problem with this 'view' query to show only 'company' and 'materials'. The problem is that it shows each entry a separate time for each material and company. I'm using this view to populate a dropdown box in a form but I would only like it to show the distinct values for each column (company/materials) - as of now if, for example, end result is the company 'Anderson' twice but with different materials for each...it shows 'Anderson' twice. I've tried using DISTINCT after the select statement for each of the two, but I don't achieve what I want.
select `b`.`company` AS `company`, `bp`.`material` AS `material`
from (((`caseys_wrdp4`.`windows_brands_products` `bp`
left join `caseys_wrdp4`.`windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`)))
join `caseys_wrdp4`.`Windows_last_submissions` `ls`)
join `caseys_wrdp4`.`windows_materials` `wm`)
where ((`bp`.`width` = round(`ls`.`width`,0))
and (`bp`.`height` = round(`ls`.`height`,0))
and (`bp`.`material` = `wm`.`name`)
and (`bp`.`type` = `ls`.`type`)
and if ((`ls`.`minimumbid` <> '0.00'),
(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),
(`bp`.`cost` <= `ls`.`maximumbid`)))
Possible answer:
Add GROUP_CONCAT and GROUP BY to your query:
select `b`.`company` AS `company`, GROUP_CONCAT(`bp`.`material`) AS `materials`
from (((`caseys_wrdp4`.`windows_brands_products` `bp`
left join `caseys_wrdp4`.`windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`)))
join `caseys_wrdp4`.`Windows_last_submissions` `ls`)
join `caseys_wrdp4`.`windows_materials` `wm`)
where ((`bp`.`width` = round(`ls`.`width`,0))
and (`bp`.`height` = round(`ls`.`height`,0))
and (`bp`.`material` = `wm`.`name`)
and (`bp`.`type` = `ls`.`type`)
and if ((`ls`.`minimumbid` <> '0.00'),
(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),
(`bp`.`cost` <= `ls`.`maximumbid`)))
GROUP BY(`b`.`company`);
This will give you back a single row for each company w/ company and materials. materials will be a comma-separated list (#Barmar). You could then parse that field for the 2nd drop down.
Example Rows:
'Anderson' 'Wood,Vinyl'
'WM' 'Metal','Plastic'
Assuming you're building a webpage, depending on how you're building the DOM, you can either use javascript str.split(), https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split, or on the server side, any language should work.
I’m trying to select duplicated nodes on a Drupal site, basically i need to select nodes that share a common ‘tnid' (translation node id), and also share the same ‘language’.
But i can’t figure out how to write the query, i think i did the first part, finding nodes with common tnid, like so
SELECT origin.nid, origin.tnid, origin.title, origin.language
FROM node AS origin
JOIN (select nid, tnid from node
group by tnid having count(tnid) > 1) common_tnid ON common_tnid.tnid = origin.tnid
#JOIN node common_lang ON common_lang.language = origin.language
AND common_lang.tnid = origin.tnid
WHERE origin.tnid != 0
Considering the language part is my big hurdle, how would i add that to the query? I tried a bunch of stuff, thus. the comment.
Try this:
SELECT
table1.nid nid,
table1.tnid tnid,
table1.language language,
table1.title title
FROM
(
SELECT *
FROM
table1
GROUP BY
tnid, language
HAVING
COUNT(*) > 1
) dupe
LEFT JOIN
table1
ON dupe.tnid = table1.tnid
AND dupe.language = table1.language
SQL Fiddle: http://sqlfiddle.com/#!9/294cc/1/0
You can try something like this
SELECT origin.id AS origin_id, common.id AS common_id
FROM node AS origin
INNER JOIN node AS common ON common.language = origin.language AND common.tnid = origin.tnid AND origin.id != common.id
I dont know if your table has id field but you can change to some field that is different in both rows