简体   繁体   English

MySQL语法有什么问题?

[英]What is wrong with mySQL Syntax?

i am using the following code to count, and to sum the values from the database. 我正在使用以下代码进行计数,并对数据库中的值求和。

$query = "SELECT
          COUNT(n.*) AS cnt_news,
          COUNT(a.*) AS cnt_adv,
          COUNT(c.*) AS cnt_comm,
          SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '0' THEN 1 ELSE 0 END) AS cnt_spam,
          SUM(a.amount) AS t_amnt,
          SUM(a.cashpaid) AS t_cpaid,
          SUM(a.balance) AS t_bal
          FROM
          news n, advertisements a, comments c";
          $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result);

the following code gives me an error, the error is 以下代码给我一个错误,错误是

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS cnt_news, COUNT(a.*) AS cnt_adv, COUNT(c.*) AS cnt_c' at line 2

if i remove the first three lines of the select query, it does not show the error instead it prints the wrong values. 如果我删除选择查询的前三行,它不会显示错误,而是会打印错误的值。

that is wrong with my code. 我的代码有问题。 ?? ??

the following code works perfectly fine for me. 下面的代码对我来说很好用。

$query = "SELECT COUNT(*) as cnt_news FROM news";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT COUNT(*) as cnt_adv FROM advertisements";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$query = "SELECT COUNT(*) as cnt_comm FROM comments";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS cnt_spam
          FROM COMMENTS c";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(a.amount) as t_amnt,
          SUM(a.cashpaid) as t_cpaid,
          SUM(a.balance) as t_bal
          FROM advertisements a";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

where am i going wrong? 我要去哪里错了?

Well i dropped the idea of making my queries into a single one, and as suggested by Col.Shrapnel i did a custom function for it, and i found it very easy to maintain the code this way. 好吧,我放弃了将查询合并为一个查询的想法,并且正如Col.Shrapnel的建议那样,我为此做了一个自定义功能,并且发现以这种方式维护代码非常容易。 thank you Col.Sharpnel i am posting the answer suggested by him. 谢谢。Sharpl Col我正在发布他建议的答案。

this is the user defined function i created. 这是我创建的用户定义函数。

function dbgetvar($query) {
             $res = mysql_query($query);
         if( !$res) {
             trigger_error("dbget: ". mysql_error(). " in " .$query);
             return false;
             }
             $row = mysql_fetch_array($res);
             if(!$row) return "";
             return $row;
             }  

and then i called my function using this code. 然后我使用此代码调用了我的函数。

     $news = dbgetvar("SELECT COUNT(*) as count FROM news");
 $comments = dbgetvar("SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS approved,
                       SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS pending,
                       SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS spam,
                       COUNT(*) AS count
                       FROM COMMENTS c");
$advertise = dbgetvar("SELECT SUM(a.amount) AS amount,
                       SUM(a.cashpaid) AS cashpaid,
                       SUM(a.balance) AS balance,
                       COUNT(*) AS count
                       FROM advertisements a");

the above code is working perfectly fine for me. 上面的代码对我来说很好用。

It looks like Mysql doesn't like that line. 看来Mysql不喜欢那条线。 Change COUNT(n.*) to COUNT(n.id) or whatever the name of that table's primary key field is. COUNT(n.*)更改为COUNT(n.id)或该表的主键字段的名称。 Do the same for a and c . ac执行相同的操作。

您不能使用count(tablename.*) ,请尝试使用count(tablename.columnname)

You can try 你可以试试

SELECT (SELECT COUNT( ) FROM news) AS cnt_news, (SELECT COUNT( ) FROM advertisements) as cnt_adv, ... 选择( 从新闻中选择COUNT( )条新闻)作为cnt_news,从(广告中选择COUNT( )条新闻 )作为cnt_adv,...

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

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