[英]SQL - Exclude a row based on another value in the same table
The application in question is a classified site. 有问题的应用程序是一个机密站点。 I'm using a query to retrieve a value in a field called "cp_price" to generate a total value for all the listings in the system.
我正在使用查询来检索名为“ cp_price”的字段中的值,以生成系统中所有列表的总值。
My goal is to exclude all listings that have a value of "yes" in the "cp_ad_sold" field. 我的目标是排除 “ cp_ad_sold”字段中值为“是”的所有列表。
Here's the query I'm using to retrieve the values: 这是我用来检索值的查询:
$raw_prices = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} AS pm
LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id
WHERE p.post_type = 'ad_listing'
AND p.ID = pm.post_id
AND pm.meta_key = 'cp_price'
" ) );
So, I am guessing I need to do a subselect? 因此,我猜我需要进行子选择吗?
I know that the key and respective value I need is stored in the postmeta table, right along with the "cp_price" field. 我知道我需要的键和相应的值与“ cp_price”字段一起存储在postmeta表中。 I also know that "cp_price" and "cp_ad_sold" share the same "post_id".
我也知道“ cp_price”和“ cp_ad_sold”共享相同的“ post_id”。
Question: How do I go about excluding the ad_listings and their respective cp_price value, based on a value of "yes" in the key of "cp_ad_sold"? 问题:如何根据关键字“ cp_ad_sold”中的“是”值来排除ad_listing及其相应的cp_price值?
Note: I'm using WordPress for this and considered posting the question over on the WordPress Stackexchange. 注意:我正在为此使用WordPress,并考虑将问题发布到WordPress Stackexchange上。 I figured this was a heavily SQL based question, though, and figured this was the most appropriate venue.
不过,我认为这是一个严重的基于SQL的问题,并且认为这是最合适的场所。
You may also want to add a condition to the WHERE clause - 您可能还想向WHERE子句添加条件-
$raw_prices = $wpdb->get_col( $wpdb->prepare( "
SELECT pm.meta_value FROM {$wpdb->postmeta} AS pm
LEFT JOIN {$wpdb->posts} AS p ON p.ID = pm.post_id
WHERE p.post_type = 'ad_listing'
AND p.ID = pm.post_id -- Do you really need it? Check LEFT JOIN ... ON clause
AND pm.meta_key = 'cp_price'
AND cp_ad_sold <> 'yes' -- Something like this
" ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.