mysql update/replace syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Burditt

    #16
    Re: mysql update/replace syntax

    >> > You are approching this from the wrong angle. You should be quering the[color=blue][color=green][color=darkred]
    >> > customer sales records and producing summary information from that.[/color][/color][/color]

    This is a hospital kitchen. Recording Personally Identifiable
    Information about a patient in a database makes that database legally
    unusable for the purpose intended for it (what kind of food they
    should order and how much they should keep on hand), and that's why
    your predecessor is in jail now: violating HIPAA requirements.
    [color=blue][color=green][color=darkred]
    >> > INSERT INTO sales (customer, product , qty) VALUES
    >> > ('$customer','$ fruit','$qty')
    >> >
    >> > SELECT fruit, sum(qty) AS numsales FROM sales GROUP BY fruit ORDER BY
    >> > numsales
    >> >
    >> > http://dev.mysql.com/doc/mysql/en/GR...Functions.html[/color]
    >>
    >> No, I am wanting to *record* what visitor does. If they buy 5 apples, I[/color]
    >want[color=green]
    >> mySQL to find the row with "apple" in it, and increment it by 5. If there[/color]
    >is[color=green]
    >> *no* row with "apple", then create a row and enter a "5" there.[/color][/color]

    It is possible to insert or update a row with one query:

    INSERT INTO sales_summary SET product = 'apple', qty = 3
    ON DUPLICATE KEY UPDATE qty = qty + 3;

    This requires that the product column has a unique key on it.

    It has the advantage that it's atomic: you don't have to do explicit
    locking but you can't get fouled up by different ordering of requests.
    (The problem with this kind of query is that you need a minimum
    version of MySQL of about 4.1 (not sure exactly which version), and
    I don't know that any other database accepts this syntax.).

    You might be able to add a "date" column to get daily totals, but
    only if you can convince management that asking the judge for
    permission is worth the risk of having him prohibit the existence
    of the database entirely.

    I use this sort of thing a lot with email white/black lists. You
    want to record, say, the sender, the number of emails from this
    sender to good addresses (but NOT what the good addresses are), the
    number of emails from this sender to bad addresses (but NOT what
    the bad addresses are), and the time of the latest email from that
    sender. If there's no entry, add one. If there is an entry,
    increment one of the counters. There is a high probability of
    simultaneous SPAMs from the same sender arriving at the same time.
    You DO NOT want to record each email: this allows spammers to
    conduct a denial-of-service attack against you by running your
    database out of disk space.

    Another process can later classify the sender as one to be blocked
    or not, in part based on the assumption that a sender who sends
    a lot of mail to mostly invalid addresses is a spammer. Other
    fields can store manual settings.
    [color=blue]
    >Yes you are. The above will tell you exactly how many apples or bananas have
    >been sold without resorting to increamenting a seperate table/record.[/color]

    Yes, but you use a lot more storage, and the personally identifiable
    information in it means you're not allowed to do any SELECTs at
    all, and has a high probability of getting the whole project cancelled.
    [color=blue]
    >Further more, with a little creativity, you can find out things like, on
    >what day which fruit sells best. How much of each fruit do you sell each
    >month? Is there a peek period for selling oranges?[/color]

    Keeping this kind of marketing information around can kill your business
    if it gets out that you're keeping it, say, because someone managed
    to steal it.

    Gordon L. Burditt

    Comment

    • CJ Llewellyn

      #17
      Re: mysql update/replace syntax

      "Gordon Burditt" <gordonb.pilrf@ burditt.org> wrote in message
      news:cgub33$6lb @library1.airne ws.net...[color=blue][color=green][color=darkred]
      > >> > You are approching this from the wrong angle. You should be quering[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > >> > customer sales records and producing summary information from that.[/color][/color]
      >
      > This is a hospital kitchen. Recording Personally Identifiable
      > Information about a patient in a database makes that database legally
      > unusable for the purpose intended for it (what kind of food they
      > should order and how much they should keep on hand), and that's why
      > your predecessor is in jail now: violating HIPAA requirements.[/color]
      -snip-

      Nobody has said that this is a hospital kitchen. HIPAA requirements do not
      apply outside of the USA. HIPAA only applies to hospital information.
      Nothing in the example mandates that personal information is to be kept.
      [color=blue]
      > Keeping this kind of marketing information around can kill your business
      > if it gets out that you're keeping it, say, because someone managed
      > to steal it.[/color]

      Every single in the company in the world keeps this type of information.
      They have to, otherwise they have great difficulty in managing their
      accounts, stock order processes, producing statutory information i.e. tax
      returns and providing good customer service.




      Comment

      • Westcoast Sheri

        #18
        Re: mysql update/replace syntax

        Michael Austin wrote:
        [color=blue]
        > Westcoast Sheri wrote:[color=green]
        > > CJ Llewellyn wrote:
        > >
        > >[color=darkred]
        > >>On Sun, 29 Aug 2004 14:18:05 +0000, Westcoast Sheri wrote:
        > >>
        > >>
        > >>>CJ Llewellyn wrote:
        > >>>
        > >>>
        > >>>>"Westcoas t Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
        > >>>>news:4131C8 7F.5BF798C7@nos pamun8nospam.co m...
        > >>>>
        > >>>>>CJ Llewellyn wrote:
        > >>>>>
        > >>>>>
        > >>>>>>"Westcoas t Sheri" <sheri_deb88@no spamun8nospam.c om> wrote in message
        > >>>>>>news:4131 5FCC.AFFCF91D@n ospamun8nospam. com...
        > >>>>>>
        > >>>>>>>To keep track of how many fruits my visitors buy, I use a mySQL
        > >>>>
        > >>>>database
        > >>>>
        > >>>>>>>(2 columns: "fruit" and "quantity").... so can we make these following
        > >>>>>>>mySQL queries work somehow?
        > >>>>>>>
        > >>>>>>>(visit or buys 5 apples):
        > >>>>>>>replac e into fruit_database set fruit = 'apple' , quantity = quantity
        > >>>>
        > >>>>+
        > >>>>
        > >>>>>>>5;
        > >>>>>>>
        > >>>>>>>(visit or buys 7 apples):
        > >>>>>>>replac e into fruit_database set fruit = 'apple' , quantity = quantity
        > >>>>
        > >>>>+
        > >>>>
        > >>>>>>>7;
        > >>>>>>>
        > >>>>>>>(visit or buys 1 grape):
        > >>>>>>>replac e into fruit_database set fruit = 'grape' , quantity = quantity
        > >>>>
        > >>>>+
        > >>>>
        > >>>>>>>1
        > >>>>>>
        > >>>>>>You are approching this from the wrong angle. You should be quering the
        > >>>>>>custome r sales records and producing summary information from that.
        > >>>>>>
        > >>>>>>INSERT INTO sales (customer, product , qty) VALUES
        > >>>>>>('$custom er','$fruit','$ qty')
        > >>>>>>
        > >>>>>>SELECT fruit, sum(qty) AS numsales FROM sales GROUP BY fruit ORDER BY
        > >>>>>>numsale s
        > >>>>>>
        > >>>>>>http://dev.mysql.com/doc/mysql/en/GR...Functions.html
        > >>>>>
        > >>>>>No, I am wanting to *record* what visitor does. If they buy 5 apples, I
        > >>>>
        > >>>>want
        > >>>>
        > >>>>>mySQL to find the row with "apple" in it, and increment it by 5. If there
        > >>>>
        > >>>>is
        > >>>>
        > >>>>>*no* row with "apple", then create a row and enter a "5" there.
        > >>>>
        > >>>>Yes you are. The above will tell you exactly how many apples or bananas have
        > >>>>been sold without resorting to increamenting a seperate table/record.
        > >>>>
        > >>>>Further more, with a little creativity, you can find out things like, on
        > >>>>what day which fruit sells best. How much of each fruit do you sell each
        > >>>>month? Is there a peek period for selling oranges?
        > >>>
        > >>>no no no. If visitor "a" buys 5 apples, then a "5" should be in the mySQL
        > >>>table. But then, an hour later, if visitor "b" buys 3 more apples, then an "8"
        > >>>should be in the mySQL table. What you are suggesting is that first there will
        > >>>be a "5" in the table....then when visitor "b" buys 3 apples, there will then
        > >>>be a "3" in the table. I really thought I worded my question very well.
        > >>>Apparantly not. Sorry.
        > >>
        > >>You question was understood. It's just your approach to the problem is
        > >>wrong.
        > >>
        > >>If you have a table that holds
        > >>
        > >>Apple 5
        > >>Orange 6
        > >>Banana 567
        > >>
        > >>This will not tell you a lot. Only in total how much of each fruit you've
        > >>sold. Unless you reset the values every week, you'll not be able to spot
        > >>trends in people's buying thus purchasing more stock than is needed.
        > >>
        > >>You have a table holding
        > >>
        > >>Fred Smith 1/1/04 Apple 2
        > >>Fred Smith 1/1/04 Banana 3
        > >>John Jones 2/1/04 Apple 5
        > >>
        > >>You can tell how many apples and bananas have been sold, by summing them
        > >>up.
        > >>[/color]
        > >
        > >
        > > Okay...I see you are putting a spin on things...and it is quite interesting. I
        > > think you are saying, "ya you have an idea...but this one is better," and I think I
        > > may agree. It is quite easy for me to create php code to make the "fred smith"
        > > example table... but the question, now, is How do I do a "SUM" select statement
        > > (e.g. something like "select SUM (quantity) from fruit_table where fruit = 'apple'"
        > > or something like that?
        > >
        > >[/color]
        >
        > Wc Sheri,
        >
        > As I stated in the previous thread, you need to have a better understanding of
        > database design BEFORE you write lots code and create databases that are of
        > little value outside of storing the data. Storing it is of no use unless you
        > can derive information from it "easily".
        >
        > I certainly hope you are not creating a new table for EACH customer as seems to
        > be inferred by the statement "It is quite easy for me to create php code to make
        > the "fred smith" example table... ".
        >
        > Create ONE table
        >
        > Customer_ID (auto increment), txdate, Last_name, First_name, Item, Qty, Total
        >
        > then in your PHP code
        >
        > insert into customer values (nextval, date(), 'Thomas', 'Fred', 'Apple', 5, 2.30);
        >
        > etc...
        >
        > Now to find out what happened today:
        >
        > select item,sum(qty),s um(total) from customer where item = 'Apple'
        > where txdate = date('somedates tring') group by item, qty, total;[/color]

        Thank you for the code. I was actually creating a separate table for not only each
        customer, but each value.
        ....(...just kidding, I just have one table :-)



        Comment

        • Westcoast Sheri

          #19
          Re: mysql update/replace syntax

          Gordon Burditt wrote:
          [color=blue][color=green][color=darkred]
          > >> > You are approching this from the wrong angle. You should be quering the
          > >> > customer sales records and producing summary information from that.[/color][/color]
          >
          > This is a hospital kitchen. Recording Personally Identifiable
          > Information about a patient in a database makes that database legally
          > unusable for the purpose intended for it (what kind of food they
          > should order and how much they should keep on hand), and that's why
          > your predecessor is in jail now: violating HIPAA requirements.
          >[color=green][color=darkred]
          > >> > INSERT INTO sales (customer, product , qty) VALUES
          > >> > ('$customer','$ fruit','$qty')
          > >> >
          > >> > SELECT fruit, sum(qty) AS numsales FROM sales GROUP BY fruit ORDER BY
          > >> > numsales
          > >> >
          > >> > http://dev.mysql.com/doc/mysql/en/GR...Functions.html
          > >>
          > >> No, I am wanting to *record* what visitor does. If they buy 5 apples, I[/color]
          > >want[color=darkred]
          > >> mySQL to find the row with "apple" in it, and increment it by 5. If there[/color]
          > >is[color=darkred]
          > >> *no* row with "apple", then create a row and enter a "5" there.[/color][/color]
          >
          > It is possible to insert or update a row with one query:
          >
          > INSERT INTO sales_summary SET product = 'apple', qty = 3
          > ON DUPLICATE KEY UPDATE qty = qty + 3;[/color]

          .....actually this is the first code I ever tried (because it's exactly what I
          wanted in the first place!), and kept getting errors! But then I realized that
          perhaps my server was not using the mySQL version that allowed this ("...blah
          blah blah syntax error near "ON DUPLICATE KEY"). That is the perfect line of
          code, though! Thanks!!!




          Comment

          • ercherry
            New Member
            • May 2006
            • 1

            #20
            REPLACE -- incrementing twice?

            I had the exact same problem as the original poster, and I didn't want all of the excess data that was suggested as imperative for collection by the responders. My problem was increased by the fact that I couldn't use that ON DUPLICATE KEY UPDATE line of code due to lack of support. (SO SAD)

            So here is what I did...

            $query="SELECT c FROM Table WHERE a= "somevalue" ;
            $result =mysql_query($q uery)OR die("error 3 - query failed".mysql_e rror());
            $myrow = mysql_fetch_arr ay($result);
            $3val = $myrow["c"];
            ++$3val;

            $query = "REPLACE INTO Table (a, b, c) VALUES ($aval, $2val, $3val)";
            $result =mysql_query($q uery)OR die("error 3 - query failed".mysql_e rror());

            ....

            I tried to simplify it a bit for reading here...

            I'm trying to increment a value before it gets replaced. The above works, but strangely, the $3val gets incremented twice.

            It could be happening somewhere else in the code, because I am using Flash remoting. It's a crazy little bug to track down, but I have a feeling that it is the REPLACE command that is doing it to me. I'm at a loss.

            I also tried stripping the

            ++$3val;

            and instead putting it directly into the query

            $query = "REPLACE INTO Table (a, b, c) VALUES ($aval, $2val, ($3val + 1))";
            $result =mysql_query($q uery)OR die("error 3 - query failed".mysql_e rror());

            Comment

            Working...