左联接的MYSQL查询

时间:2018-08-03 14:32:35

标签: php mysql

我有以下查询,我想再添加一个参数。该查询与Woocommerce产品有关。我只需要有in_stock的产品。因此原始查询如下所示:

 $query = mysql_query("SELECT `post_author`, `guid`, `post_status`, `post_type`, `meta_key`, `meta_value`, `user_id`, `user_nicename`, count(*) FROM `3XvmI7jKh0_posts` as p left join `3XvmI7jKh0_usermeta` as um on um.user_id = p.post_author left join `3XvmI7jKh0_users` as un on un.ID = p.post_author where um.meta_key = 'sow_vendor_geocoding' and p.post_status = 'publish' and p.post_type = 'product'  GROUP BY `post_author`")or die(mysql_error());

我的上一次尝试是这样的,但是它确实会产生一个错误。我在做什么错了?

 $query = mysql_query("SELECT `post_author`, `guid`, `post_status`, `post_type`, `meta_key`, `meta_value`, `user_id`, `user_nicename`, count(*) FROM `3XvmI7jKh0_posts` as p left join `3XvmI7jKh0_usermeta` as um on um.user_id = p.post_author left join `3XvmI7jKh0_users` as un on un.ID = p.post_author left join `3XvmI7jKh0_postmeta` as ur on ur.post.id = p.id where um.meta_key = 'sow_vendor_geocoding' and p.post_status = 'publish' and p.post_type = 'product' and ur.meta_value = "in_stock" GROUP BY `post_author`")or die(mysql_error());

2 个答案:

答案 0 :(得分:1)

您正在无意间关闭引号:

"SELECT a FROM foo WHERE b = "bar";"

相反,您要么想转义双引号,要么复制其他子句并使用单引号:

"SELECT a FROM foo WHERE b = 'bar';"

答案 1 :(得分:1)

您在查询中使用双引号,该查询本身由双引号约束。您需要转义"in_stock"\"in_stock\")周围的引号,或将其替换为单引号'