Question

I need to compare records in the same mysql magento table. Is there a way to compare 2 records in the same table, by comparing the url_key to the url? I posted the information below that coresponds to what I'm looking for.

I'm hoping this will help out others who work on Magento's Database as well, as I have posted my 2 previous queries as to how I compared 2 mysql tables to make sure the records were created for categories in magento. I have found that Magento will fail to run flat file reindexing if url-keys and urls do not match. I have ran a few checks to make sure that my url's are correct in both my catalog_category_entity_varchar table as well as my core_url_rewrite table. I intially ran:

SELECT * FROM catalog_category_entity_varchar c2t WHERE NOT EXISTS ( SELECT * FROM core_url_rewrite c WHERE c.category_id = c2t.entity_id )

to make sure all of our categories are also entered into core_url rewrite. Then I ran another query to make sure that all of the urls in both tables matched with:

SELECT * FROM core_url_rewrite c WHERE NOT EXISTS ( SELECT * FROM catalog_category_entity_varchar c2t WHERE c2t.entity_id = c.category_id AND c2t.value = c.request_path
)

Now I would like to run one more query to ensure that the url's on catalog_category_entity_varchar are also correct to it's corresponding url key, but I'm completely stuck on it and have no idea how to write the statement.

Basically the catalog_category_entity_varchar table looks like this:

catalog_category_entity_varchar:

Record 1:
value_id:             68
entity_type_id:      3
attribute_id:        43
store_id:               0
entity_id:             10
value:                shop-by

Record 2:
value_id:             73
entity_type_id:      3
attribute_id:        57
store_id:               0
entity_id:             10
value:                shop-by.html

The entity_id is 10 for both records. The attribute ID for url_key is 43 and for the url ID is 57. I imagine that these are what I'll need to use for to identify what I'm comparing.

So basically I'll need to run a query that will matches the entity id's to each other and then compare's the url-key to the url itslef to make sure that it contains the url key. It will have to strip the .html as well as any other part of the url code since records deeper than first level will look something like catalog/shirts/shop-by.html.

Record 3:
value_id:             637
entity_type_id:      3
attribute_id:        57
store_id:               0
entity_id:             88
value:                catalog/shirts/shop-by.html

Also, there will be records that also contain this URL-Key, but I'm assuming that since the query will be based on it's primary key (entity_id), we won't have to worry about that.

I apologize if I haven't written this in the correct format, as I'm still new to this forum. I appreciate in advance for the help. If there's anything unclear or more information is needed, please let me know.

Was it helpful?

Solution

Check this query, it's a little ugly but it worked as far as I could tell.

SELECT `t1`.*, `t2`.`value` AS `url_path`
FROM `catalog_category_entity_varchar` AS `t1`
LEFT JOIN (
    SELECT `catalog_category_entity_varchar`.`value`,
        `catalog_category_entity_varchar`.`entity_id`,
        `catalog_category_entity_varchar`.`attribute_id`
    FROM `catalog_category_entity_varchar`
) AS `t2`
ON `t1`.`entity_id`=`t2`.`entity_id`
WHERE `t1`.`attribute_id` = 43
AND `t2`.`attribute_id` = 57
AND `t1`.`value` != `t2`.`value`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top