MySQL:使用子查询返回错误的null

时间:2019-08-08 10:28:05

标签: mysql

有关以下MySQL查询的某些信息已损坏。我包括用于上下文的PHP:   受保护的函数scanAgainstUrlTargets(array $ urlData){     全局$ wpdb;

$urlMatch = $wpdb->get_results("
  SELECT *
  FROM {$wpdb->prefix}delayedCoupons_targets t
  WHERE t.targetUrl = {$urlData['rawUrl']}
  AND t.displayThreshold < (
    select count(*)
    from {$wpdb->prefix}delayedCoupons_visits v
    where v.urlVisited = {$urlData['rawUrl']}
    as visitCount)
  AND visitCount < t.displayThreshold + t.offerCutoff
");

 return $urlMatch;
}

我在$ wpdb-> last_error中看到我的调试器的内联错误:

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 '://localhost/wptest2/wp-admin/plugins.php?
plugin_status=all&paged=1&s
      AND ' at line 3

这是$ wpdb-> last_query的值:

  SELECT *
  FROM wp_delayedCoupons_targets t
  WHERE t.targetUrl = http://localhost/wptest2/wp- admin/plugins.php?plugin_status=all&paged=1&s
  AND t.displayThreshold < (
    select count(*)
    from wp_delayedCoupons_visits v
    where v.urlVisited = http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s
    as visitCount)
  AND visitCount < t.displayThreshold + t.offerCutoff

$ urlData在调试器中显示此内容,确认变量正确传递:

rawUrl = http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s
urlRoot = http://localhost/wptest2/wp-admin/plugins.php
queryString = ?plugin_status=all&paged=1&s

我很难过这个。如果我不得不猜测,也许与我如何将子查询别名为visitCount有关,因为这对我来说是新的。 有人看到这个问题吗?

也尝试过:

在CLI中运行查询,并使用单引号或反引号将两个URL字符串隔开。在这两种情况下,Error均指向打开的子查询括号,如下所示:

owner@G700:/var/www/html/wptest2/wp-content/plugins/delayedCoupons$ 
SELECT * FROM wp_delayedCoupons_targets t WHERE t.targetUrl = 
'http://localhost/wptest2/wp-admin/plugins.php? 
plugin_status=all&paged=1&s' AND t.displayThreshold < ( select 
count(*) from wp_delayedCoupons_visits v where v.urlVisited = 
'http://localhost/wptest2/wp-admin/plugins.php? 
plugin_status=all&paged=1&s' as visitCount) AND visitCount < 
t.displayThreshold + t.offerCutoff

bash: syntax error near unexpected token `('

更新此查询会产生一个新错误:

$urlMatch = $wpdb->get_results("
  SELECT *, (select count(*) from wp_delayedCoupons_visits v where v.urlVisited = 'localhost/wptest2/wp-admin/plugins.php? plugin_status=all&paged=1&s' as visitCount)
  FROM {$wpdb->prefix}delayedCoupons_targets t
  WHERE t.targetUrl = {$urlData['rawUrl']}
  AND t.displayThreshold < visitCount
  AND visitCount < t.displayThreshold + t.offerCutoff
");

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 visitCount)
      FROM wp_delayedCoupons_targets t
      WHERE t.targetUrl = ' at line 1

2 个答案:

答案 0 :(得分:1)

要一次性使用子查询,请使用以下方法:

SELECT t.*,visitData.count FROM {$wpdb->prefix}delayedCoupons_targets t, (select count(*) as count from wp_delayedCoupons_visits v where v.urlVisited = 'demoPosition1') as visitData WHERE t.targetUrl = 'stringPosition2' AND t.displayThreshold < visitData.count AND visitData.count < t.displayThreshold + t.offerCutoff

要在Where中使用子查询(这会很慢,效果不好):

SELECT t.* FROM {$wpdb->prefix}delayedCoupons_targets t WHERE t.targetUrl = 'stringPosition2' AND t.displayThreshold < (select count(*) from wp_delayedCoupons_visits v where v.urlVisited = 'demoPosition1') AND (select count(*) from wp_delayedCoupons_visits v where v.urlVisited = 'demoPosition1') < t.displayThreshold + t.offerCutoff

答案 1 :(得分:0)

在子查询中的where子句后面有一个as visitCount,可能需要count(*)的别名,但不能在compare子查询中使用别名,因此应重复子查询代码而不是别名

   $urlMatch = $wpdb->get_results("
      SELECT *
      FROM {$wpdb->prefix}delayedCoupons_targets t
      WHERE t.targetUrl = {$urlData['rawUrl']}
      AND t.displayThreshold < (
        select count(*)
        from {$wpdb->prefix}delayedCoupons_visits v
        where v.urlVisited = {$urlData['rawUrl']}
        )
      AND (
        select count(*)
        from {$wpdb->prefix}delayedCoupons_visits v
        where v.urlVisited = {$urlData['rawUrl']}
        ) < t.displayThreshold + t.offerCutoff
    ");