mysql - Select distinct column value from date range -


i created sqlfiddle outlines i'm trying do:

http://sqlfiddle.com/#!9/41f3c/2

basically, need search unique posts in table contains meta information. meta in instance series of dates represent exclusions (think of booking system hotel).

i pass in start , end date. want find post_id not contain date falls in range. i'm close, can't quite figure out.

select distinct post_id  wp_facetwp_index facet_name = 'date_range' , facet_value not between '$start_date' , '$end_date' 

this works if excluded dates in table in range, if out of range, still post_id.

thanks looking.

do not forget, in sql, filters (where clause, etc.) applied on record basis. each record evaluated independantly others.

so, since

(1, 511, 'date_range', 'cf/excluded_dates', '2015-07-31', '2015-07-31')

validates condition, 511 returned.

since post_id not unique, need proceed exclusion on sets, opposed exclusion on records you're doing right now.

here solution (adjusted fiddle here: http://sqlfiddle.com/#!9/41f3c/7)

select distinct i1.`post_id`  `wp_facetwp_index` i1 i1.`facet_name` = 'date_range' , not exists (     select 1     `wp_facetwp_index` i2              i2.`facet_value` between '$start_date' , '$end_date'     , i2.`facet_name` = 'date_range'     , i2.`post_id` = i1.`post_id` ) 

the subquery right after exists ( subset of rows. evaluated negatively not exists based on junction i2.post_id = i1.post_id. negative intersection.

working on exluding records not work if tuple need indentify not unique.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -