Mysql_query () cannot parse SQL normally

Source: Internet
Author: User
Mysql_query () cannot parse an SQL statement normally, and it is normal to directly run it in mysql.
However, if mysql_query in php is used to run the SQL statement, the result is different. Is it because mysql_query cannot be parsed?




The SQL statement is as follows:



INSERT INTO analysis.caomei_of_list_pvuv  (region,isp,uv,pv,date)  SELECTtb2.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 ;


Reply to discussion (solution)

Nsert into analysis. caomei_of_list_pvuv
(Region, isp, uv, pv, date)
SELECT
Tb2.region, tb2.isp, tb2.uv, tb2.pv, '2017-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 = '2017-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 = '2017-05-14'
) AS tb1

ON tb2.region = tb1.region
WHERE
Tb1.region IS NULL
AND tb2.region IS NOT NULL

This is not allowed

Nsert into analysis. caomei_of_list_pvuv
(Region, isp, uv, pv, date)
SELECT
Tb2.region, tb2.isp, tb2.uv, tb2.pv, '2017-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 = '2017-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 = '2017-05-14'
) AS tb1

ON tb2.region = tb1.region
WHERE
Tb1.region IS NULL
AND tb2.region IS NOT NULL

This is not allowed



However, mysql runs normally to obtain results.
Php can also be executed, but the result shows an offset !!

The question is not whether there is a syntax error.
Control logic is a cyclical paradox.

But I think there should be no logic problem. First, check the results. Then write the result to yourself.
The results of php running are different from those of mysql running !!

Do you want to avoid this logic as much as possible? otherwise, this unexpected result will appear.

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

Assume that one entry is inserted during the first execution.
The second execution inserts two
The third execution inserts four

I understand ...................................

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.