skip to Main Content

Hope some of you pros can help me out on this sql / php issue.

The short version:

I need to add members to a task-database. So I have memberlist, it loop through each member and runs below sql.

I need to run an SQL statement that is to exit after first update / execution where it hits the parameters. So I need some kind of return for each time the sql updates a field?

Pseudocode:

Update this column
condition 1
condition 2
after first execution exit

Current sql:

UPDATE calendar
SET spil1 = '$temp'
WHERE spil1 IS NOT NULL
AND
(dayname = 'Lørdag'
OR dayname = 'Søndag')

// now exit if the above is met and the sql update was executed.

So the problem is I cannot make it stop (tried limit, top etc)
How is this made with SQL? or is there a smart way to condition it in the PHP loop before executing the script?

3

Answers


  1. Do you have an id column in calendar? If yes use the following query (Not tested):

    UPDATE calendar
    SET spil1 = '$temp'
    WHERE id =
    (SELECT id FROM
    (SELECT * FROM calendar
    WHERE spil1 IS NOT NULL
    AND
    (dayname = 'Lørdag'
    OR dayname = 'Søndag')
    LIMIT 1)T)
    

    What this query does, it brings the first record that applies to your condition, and then update that record the way you want it

    Login or Signup to reply.
  2. if you are using any unique id put this code at the end of your query..
    good luck.

    AND unique_id IN ( SELECT unique_id FROM calendar order by unique_id ASC LIMIT 1 )
    
    Login or Signup to reply.
  3. I’m pretty sure you want to assign a different member to each NULL value in the calendar table. This is tricky. It requires enumerating the rows in each table for the join — and assumes a unique id in the calendar table.

    update calendar c join
           (select c.*,
                   row_number() over (order by c2.pil1) as seqnum
            from calendar c2
            where c2.pil1 is not null and
                  c2.dayname in ('Lørdag', 'Søndag')
           ) c2
           on c.calendar_id = c2.calendar_id join  -- the unique id
           (select ml.*,
                   row_number() over (order by ml.member_id) as seqnum
            from memberlist ml
           ) ml
           on ml.seqnum = c2.seqnum
        set c.spil1 = ml.member_id;
    

    I also suspect that you want the condition for the calendar table to be IS NULL rather than IS NOT NULL, but this is the logic you have in the question.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search