>  기사  >  백엔드 개발  >  mysql_query()不能正常解析sql

mysql_query()不能正常解析sql

WBOY
WBOY원래의
2016-06-23 13:57:02960검색


一个sql语句,直接放到mysql里运行是正常的
但用php的mysql_query运行该sql后结果却不同,会是mysql_query不能解析的原因吗?




sql如下:



		INSERT INTO analysis.caomei_of_list_pvuv  									(region,isp,uv,pv,date)  									SELECT										tb2.region,tb2.isp,tb2.uv,tb2.pv,'2014-05-14' AS date   									FROM   									(										SELECT 											tb.region,tb.isp, COUNT(tb.mac) AS uv,											SUM(tb.mac_num) AS pv  										FROM 											caomei_20140515.caomei_1 AS tb 										WHERE 											tb.date = '2014-05-14'  										GROUP BY tb.region, tb.isp 									) AS tb2  									LEFT JOIN 									(										SELECT 											tb6.region,tb6.isp,tb6.uv,tb6.pv 										FROM 											analysis.caomei_of_list_pvuv AS tb6 										WHERE 											tb6.date='2014-05-14'   									) AS tb1 																				ON tb2.region = tb1.region 									WHERE 										tb1.region IS NULL  										AND tb2.region IS NOT NULL 									;


回复讨论(解决方案)

NSERT INTO  analysis.caomei_of_list_pvuv  
                            (region,isp,uv,pv,date)  
                            SELECT
                                tb2.region,tb2.isp,tb2.uv,tb2.pv,'2014-05-14' AS date   
                            FROM   
                            (
                                SELECT 
                                    tb.region,tb.isp, COUNT(tb.mac) AS uv,
                                    SUM(tb.mac_num) AS pv  
                                FROM 
                                    caomei_20140515.caomei_1 AS tb 
                                WHERE 
                                    tb.date = '2014-05-14'  
                                GROUP BY tb.region, tb.isp 
                            ) AS tb2  
                            LEFT JOIN 
                            (
                                SELECT 
                                    tb6.region,tb6.isp,tb6.uv,tb6.pv 
                                FROM 
                                     analysis.caomei_of_list_pvuv AS tb6 
                                WHERE 
                                    tb6.date='2014-05-14'   
                            ) AS tb1    
                                 
                            ON tb2.region = tb1.region 
                            WHERE 
                                tb1.region IS NULL  
                                AND tb2.region IS NOT NULL 
 
这是不允许的

NSERT INTO  analysis.caomei_of_list_pvuv  
                            (region,isp,uv,pv,date)  
                            SELECT
                                tb2.region,tb2.isp,tb2.uv,tb2.pv,'2014-05-14' AS date   
                            FROM   
                            (
                                SELECT 
                                    tb.region,tb.isp, COUNT(tb.mac) AS uv,
                                    SUM(tb.mac_num) AS pv  
                                FROM 
                                    caomei_20140515.caomei_1 AS tb 
                                WHERE 
                                    tb.date = '2014-05-14'  
                                GROUP BY tb.region, tb.isp 
                            ) AS tb2  
                            LEFT JOIN 
                            (
                                SELECT 
                                    tb6.region,tb6.isp,tb6.uv,tb6.pv 
                                FROM 
                                     analysis.caomei_of_list_pvuv AS tb6 
                                WHERE 
                                    tb6.date='2014-05-14'   
                            ) AS tb1    
                                 
                            ON tb2.region = tb1.region 
                            WHERE 
                                tb1.region IS NULL  
                                AND tb2.region IS NOT NULL 
 
这是不允许的



但是mysql是能正常执行得到结果。
php也能执行,只是结果出现偏移了!!

问题不在于是否有语法错
而在于控制逻辑是循环悖论

但我觉得这样应该没有逻辑问题啊,先查出结果。然后将结果写入自己。
最主要是  php运行的结果和mysql运行的结果不同!! 

是要尽量避免这种逻辑吗,否则就会出现这种意想不到的结果啊

INSERT INTO a (version)SELECT version FROM a WHERE id=1;

假定第一次执行插入了 1 条
那么第二次执行就会插入 2 条
第三次执行就会插入 4 条

我知道了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.