简体   繁体   English

从MS Access转换为mySQL

[英]Converting from MS Access to mySQL

I am working with Prestashop 1.6.0.6, it appears to have a bug where randomly the virtual product file disappears and is no longer available for download and needs to be re-uploaded. 我正在使用Prestashop 1.6.0.6,它似乎有一个错误,其中虚拟产品文件随机消失,不再可下载,需要重新上传。 So I am trying to write a query that will display all active products that have virtual products and whether that virtual product is active. 因此,我试图编写一个查询,以显示具有虚拟产品的所有活动产品以及该虚拟产品是否处于活动状态。 In some instances since I have uploaded a second time the virtual product is active can be 0 or 1, by summing I can get the final active status. 在某些情况下,由于我第二次上传,虚拟产品的活动状态可以为0或1,通过求和可以得出最终的活动状态。

I have found all the tables I need to write the query; 我已经找到了编写查询所需的所有表;

ps_product_lang->id_product
ps_product_lang->name

ps_product->id_product
ps_product->is_virtual
ps_product->active

ps_product_download->id_product
ps_product_download->display_filename
ps_product_download->filename
ps_product_download->active

I have limited mySQL query knowledge so normally write the query in MS Accesss then use the SQL view and copy to mySQL and normally it works with a little tweaking. 我对mySQL查询的知识有限,因此通常在MS Access中编写查询,然后使用SQL视图并复制到mySQL,通常只需稍作调整即可。 In this instance I am using grouping, first and summing (is virtual product active) and can't get the query to work. 在这种情况下,我正在使用分组,首先和求和(虚拟产品处于活动状态),并且无法使查询正常工作。 I keep getting syntax errors. 我不断收到语法错误。 This is the query I am using; 这是我正在使用的查询;

SELECT ps_product_lang.id_product, 
       First(ps_product_lang.name) AS FirstOfname, 
       First(ps_product.is_virtual) AS FirstOfis_virtual, 
       First(ps_product.active) AS FirstOfactive, 
       First(ps_product_download.display_filename) AS FirstOfdisplay_filename, 
       First(ps_product_download.filename) AS FirstOffilename, 
       Sum(ps_product_download.active) AS SumOfactive
FROM (ps_product 
RIGHT JOIN ps_product_lang ON ps_product.id_product = ps_product_lang.id_product) 
LEFT JOIN ps_product_download ON ps_product.id_product = ps_product_download.id_product
GROUP BY ps_product_lang.id_product
HAVING (((First(ps_product.is_virtual))=1) AND ((First(ps_product.active))=1));

Any help to get the query working would be appreciated. 任何帮助使查询正常工作的帮助将不胜感激。

First, rewrite the query using table aliases and fixing the outer joins in the from clause. 首先,使用表别名重写查询,并修复from子句中的外部联接。 I find it quite hard to understand the logic with queries mix outer join types. 我发现很难理解查询混合外部联接类型的逻辑。 Because the query has columns in the product table in the having clause, the first join is really an inner join anyway. 因为查询在having子句的product表中有列,所以第一个联接实际上还是一个内部联接。 And, because the second join uses the product table and not the product lang table, this is also an inner join. 并且,因为第二个联接使用产品表而不是产品lang表,所以这也是一个内部联接。 This is the structure of your query: 这是查询的结构:

SELECT p.id_product, 
       First(l.name) AS FirstOfname, 
       First(p.is_virtual) AS FirstOfis_virtual, 
       First(p.active) AS FirstOfactive, 
       First(pd.display_filename) AS FirstOfdisplay_filename, 
       First(pd.filename) AS FirstOffilename, 
       Sum(pd.active) AS SumOfactive
FROM ps_product_lang l JOIN
     ps_product p
     ON p.id_product = l.id_product JOIN
     ps_product_download pd
     ON p.id_product = pd.id_product
GROUP BY l.id_product
HAVING First(p.is_virtual) = 1) AND First(p.active) = 1;

I don't fully understand the data structure. 我不完全了解数据结构。 If I were to assume that product.id_product is a unique key (which would be a normal naming convention), then you do not need any aggregation functions on things for that table. 如果我假设product.id_product是唯一键(这是正常的命名约定),那么您不需要该表的任何聚集函数。 However, your question suggests that this is not unique. 但是,您的问题表明这不是唯一的。

You can implement first using a trick in MySQL, combining substring_index() and group_concat() . 您可以first在MySQL中使用技巧来实现,结合使用substring_index()group_concat()

However, to get you started, I think group_concat() might be more helpful. 但是,为了让您入门,我认为group_concat()可能会更有帮助。 So, I would recommend that you start with this query: 因此,我建议您从以下查询开始:

SELECT l.id_product, 
       group_concat(l.name) AS allnames, 
       group_concat(p.is_virtual) AS allis_virtuals, 
       group_concat(p.active) AS allactives, 
       group_concat(pd.display_filename) AS alldisplay_filenames, 
       group_concat(pd.filename) AS allfilenames, 
       Sum(pd.active) AS SumOfactive
FROM ps_product_lang l JOIN
     ps_product p
     ON p.id_product = l.id_product JOIN
     ps_product_download pd
     ON p.id_product = pd.id_product
GROUP BY l.id_product
HAVING SUM(p.is_virtual = 1) > 0 AND SUM(p.active = 1) > 0;

If this doesn't do what you want, then you should be able to tweak it. 如果这不能满足您的要求,那么您应该可以对其进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM