简体   繁体   中英

Combining two SQL queries to create an automatic procedure?

I've visited this forum many times to find answers to my questions but this is my first post. It's quite a specific question so couldn't find what I was looking for by searching.

My SQL is quite limited, i've just learnt what i've needed to as I've gone along but this is well over my head!

I run a free pub advertising website called Our-Pub.co.uk. Pubs can add their upcoming events to their individual pages as well as to a comprehensive Pub Guide. This is a completely free service, supporting both pubs and pub goers. www.our-pub.co.uk/TPG

I use WordPress for my backend. Some of the events added are weekly / fortnightly events and I would like these to re-publish automatically. I was thinking this could be done by running a stored procedure to check for events that have passed and that are marked as weekly /fortnightly then adding 604800 (or 604800x2) seconds to them.

At the moment I have 2 SQL queries that I run, the first to find all events marked as weekly or fortnightly...

SELECT ID, post_title, wpr.object_id, wp_redhouseterms.name
FROM wp_redhouseterms
INNER JOIN wp_redhouseterm_taxonomy ON wp_redhouseterms.term_id = wp_redhouseterm_taxonomy.term_id
INNER JOIN wp_redhouseterm_relationships wpr ON wpr.term_taxonomy_id = wp_redhouseterm_taxonomy.term_taxonomy_id
INNER JOIN wp_redhouseposts ON ID = wpr.object_id 
WHERE taxonomy = 'category'
AND wp_redhouseterms.term_id=18
OR wp_redhouseterms.term_id=25
AND post_type = 'post'
ORDER BY `wpr`.`object_id` DESC

This returns a list of posts which are marked weekly / fortnightly with their post IDs and whether they are Weekly or Fortnightly events. I can then use this list of post IDs in my next query to return a list of existing event time values...

SELECT meta_id, post_id, meta_value 
FROM  `wp_redhousepostmeta` 
WHERE meta_key LIKE 'event_date_time'
AND (post_id LIKE  '1726'
OR post_id LIKE  '2296'
OR post_id LIKE  '1655'
OR post_id LIKE  '2373'
OR post_id LIKE  '2371'
OR post_id LIKE  '1650'
OR post_id LIKE  '1652'
OR post_id LIKE  '3036'
OR post_id LIKE  '1580'
OR post_id LIKE  '2375'
OR post_id LIKE  '4128'
OR post_id LIKE  '4132'
....etc....
)

I then update this list manually, adding either 604800 to weekly events or 1209600 to fortnightly events.

This was quite manageable at first but the list is growing and is now becoming a real pain to update!

Could any of you SQL wizzards advise me how I could combine these two scripts and put in a trigger to run at say 3am every morning to automatically update the events?

I have had a go myself but have had no luck, to be honest, I'm lost!

Any help would be hugely appreciated!

Thanks, Mark Our-Pub.co.uk

Assuming 18 is weekly and 25 is fortnightly, the following should update the events appropriately:

UPDATE wp_redhousepostmeta rh
INNER JOIN (
    SELECT ID, wt.term_id
    FROM wp_terms wt
    INNER JOIN wp_term_taxonomy wtt ON wt.term_id = wtt.term_id
    INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wtt.term_taxonomy_id
    INNER JOIN wp_posts ON ID = wpr.object_id 
    WHERE taxonomy = 'category'
      AND (wt.term_id=18 OR wt.term_id=25)
      AND post_type = 'post'
    ) t ON rh.post_id = t.ID
SET rh.meta_value = rh.meta_value + CASE WHEN t.term_id = 18 THEN 604800
                                         WHEN t.term_id = 25 THEN 1209600 END
WHERE rh.meta_key = 'event_date_time';

Test it first by running the following and checking everything looks right:

SELECT t.id 
    ,t.term_id
    ,rh.meta_value
    ,rh.meta_value + CASE WHEN t.term_id = 18 THEN 604800
                          WHEN t.term_id = 25 THEN 1209600 END AS new_meta_value
FROM wp_redhousepostmeta rh
INNER JOIN (
    SELECT ID, wt.term_id
    FROM wp_terms wt
    INNER JOIN wp_term_taxonomy wtt ON wt.term_id = wtt.term_id
    INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wtt.term_taxonomy_id
    INNER JOIN wp_posts ON ID = wpr.object_id 
    WHERE taxonomy = 'category'
      AND (wt.term_id=18 OR wt.term_id=25)
      AND post_type = 'post'
    ) t ON rh.post_id = t.ID
WHERE rh.meta_key = 'event_date_time'

SQLFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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