1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to run SQL command through cron

Discussion in 'Programming' started by southusa, May 20, 2010.

  1. #1
    Hello . I have a question about mysql
    I got a query to delete duplicate content on my websites and i would like to be able to schedule cron jobs to run the queries twice a day.
    Currently I have to do it through phpmyadmin .

    Could anybody please help me with this matter ?

    Thanks!

    below is a code:

    --------------

    DELETE bad_rows.*
    from wp_posts as bad_rows
    inner join (
    select post_title, MIN(id) as min_id
    from wp_posts
    group by post_title
    having count(*) > 1
    ) as good_rows on good_rows.post_title = bad_rows.post_title
    and good_rows.min_id <> bad_rows.id
     
    southusa, May 20, 2010 IP
  2. lvtim

    lvtim Well-Known Member

    Messages:
    291
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #2
    You need to put those SQL commands into a PHP page first, then call cron to run that particular PHP file periodically.
     
    lvtim, May 20, 2010 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    just put this script into a php or any archiving file then run this under linux cron jobs
     
    bartolay13, May 21, 2010 IP
  4. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Php is definitely a way but i suppose you could using it directly as a mysql command.
    try this:
    mysql -h hostname -u username -p password -e "QUERYHERE"
    Code (markup):
    let us know if it works!
     
    iama_gamer, May 21, 2010 IP
  5. southusa

    southusa Member

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Could one of you experts PLEASE tell me why the following doesnt work...

    1-i made a php file with the following inside :

    $sql = "DELETE bad_rows.*\n"
    . "from wp_posts as bad_rows\n"
    . "inner join (\n"
    . "select post_title, MIN(id) as min_id\n"
    . "from wp_posts\n"
    . "group by post_title\n"
    . "having count(*) > 1\n"
    . ") as good_rows on good_rows.post_title = bad_rows.post_title\n"
    . "and good_rows.min_id <> bad_rows.id";

    Then i set up cron "10 * * * * lynx -dump http://domain.com/file.php"

    it didnt work.
    Please ,somebody help.

    Thankyou
     
    southusa, May 26, 2010 IP
  6. southusa

    southusa Member

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Hi again.
    I made a php file to connect to db but it still doesnt work. Duplicate posts are still present.

    Please help me with sql synthax below:


    <?php
    mysql_connect("localhost", "x3_coffe", "password") or die(mysql_error());
    mysql_select_db("x3_coffe") or die(mysql_error());
    $sql = "DELETE bad_rows.*
    from wp_posts as bad_rows
    inner join (
    select post_title, MIN(id) as min_id
    from wp_posts
    group by post_title
    having count(*) > 1
    ) as good_rows on good_rows.post_title = bad_rows.post_title
    and good_rows.min_id <> bad_rows.id";
    ?>
     
    southusa, Jun 14, 2010 IP
  7. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #7
    a) You are better off using the command line PHP client and not lynx to do this; its what it was built for....

    b) You syntax above is incorrect because by /default/ wordpress will automatically not allow duplicate posts. (unless you are inserting them yourself and have broken the rule). If you are doing this, stop, and recode what is broken alreadt rather than trying to clean up afterwards.

    E.G If you have a title of "this is a test" it will become "this-is-a-test". The next one you try to add will be "this-is-a-test-2". This is why your query is most likely not working.

    c) Also, your subselect is wrong. See above for reasons why
     
    lukeg32, Jun 14, 2010 IP