Jump to content

SQL_F1

Members
  • Posts

    10
  • Joined

  • Last visited

    Never

Posts posted by SQL_F1

  1. or

    [code]
    INSERT IGNORE INTO `testbd`.`ARTIKEL`
    SELECT *
    FROM `testdb2`.`ARTIKEL`
    [/code]
    to insert ALL the missing ones
    although it seems you want to be inserting only the newer ones:
    [code]

    INSERT INTO `testbd`.`ARTIKEL`
    SELECT *
    FROM `testdb2`.`ARTIKEL`
    WHERE `testdb2`.`ARTIKEL`.`ARTNUM` >
    (SELECT MAX( `testdb`.`ARTIKEL`.`ARTNUM` )
        FROM  `testdb`.`ARTIKEL`
    )
    [/code]
  2. or

    [code]
    INSERT IGNORE INTO `testbd`.`ARTIKEL`
    SELECT *
    FROM `testdb2`.`ARTIKEL`
    [/code]
    to insert ALL the missing ones
    although it seems you want to be inserting only the newer ones:
    [code]

    INSERT INTO `testbd`.`ARTIKEL`
    SELECT *
    FROM `testdb2`.`ARTIKEL`
    WHERE `testdb2`.`ARTIKEL`.`ARTNUM` >
    (SELECT MAX( `testdb`.`ARTIKEL`.`ARTNUM` )
        FROM  `testdb`.`ARTIKEL`
    )
    [/code]
  3. DELETE FROM rl_mail WHERE msgid IN (SELECT msgid FROM rl_mail WHERE touid = 20 ORDER BY date DESC, time DESC LIMIT 50,999999999)

    What's wrong with:
    [code]
    DELETE FROM rl_mail
            WHERE touid = 20
        ORDER BY date DESC, time DESC
    LIMIT 50,999999999

    [/code]

    Curious:How can there be multiple users on the same msgid?
  4. The purpose of the gCount field is unclear from your query so I've guessed that gATTENDING is the gCOUNT of people who have RSVP'd positively.

    COUNT(*) counts all records in the result set.
    COUNT(<VALUE>) COUNTS non-NULL instances of <VALUE>
    To "count" the number of qualifying records, use SUM(IF(<boolean expression>, 1, 0))

    [code]$result  = mysql_query("SELECT
    SUM(gCOUNT) AS gINVITED,
    COUNT(*) AS gPARTIES,
    SUM(IF(gRSVP=1, 1, 0)) AS gRSVPED,
    SUM(IF(gRSVP=1, gCOUNT, 0))AS gATTENDING
    FROM guests");[/code]
  5. Hi Jeff
    You're in luck - I'm using this example in a SQL class I'm preparing so I have some time to devote to the problem

    There are 2 problems:
    1) We have been using  a JOIN when we should not have JOINed or JOINed in another way.  That resulted in duplicates which we then suppressed in a random way because of problem #2

    2) There is a hole in the business logic of the query.

    #2 first because #1 cannot be solved without knowing what we actually need.

    You have a Members table, probably with unique MemberID's/1 row per member in a 1 to 0, 1 or many relationwhip with a Services table joined on MemberID.

    I presume that most Members have ranked 1 or more Services and some have not ranked any (because you used a left join)

    So you have Members with multiple rankings, one for each Service. 
    [b]Which service ranking do you want?[/b]

    problem #1
    Provide the actual result set from your query - I can't tell in what order it [u][b]is[/b][/u] appearing in.

    There's no way it will not appear in res.ResRanked ASC, mem.MemberTypeID ASC order in the full query you set out (with the ORDER BY res.ResRanked ASC, mem.MemberTypeID ASC ) - tinyints for values 1 - 30 are just fine - varchar will be even worse (1, 10, 11 ... 2, 21, 22..)

    The problem with the JOIN and GROUP BY/DISTINCT is that there is no specifying which ranking you want
    GROUP BY should only be used with aggregate functions (MIN, MAX  ) because they specifiy which values you want for each value grouped - if they can't the GROUP BY is inappropriate.

    The other problem with the query is that it may be the wrong way around.
    Members are being identified by location and Services provided, then those members are listed with a ranking from the services provided table.

    The following seems to be what should is required
    [code]SELECT
    res.Service, res.ResRanked, 
    mem.MemberID, mem.FirstName, mem.MiddleName, mem.LastName,
    mem.CompanyName, mem.MemberTypeID,
    mem.MemberLevel, mem.Addr1, mem.Addr2, mem.City, mem.State, mem.Zip,
    mem.Work, mem.Fax, mem.Email
      FROM
    ResServicesProvided AS res
    INNER JOIN ASIDMembers AS mem 
      ON (res.MemberID=mem.MemberID)
    WHERE mem.MemberID IN ($members_by_location3)
    ORDER BY res.ResRanked;
    [/code]

    If this is not what is intended (see problem #2) then there must be some criteria missing from the WHERE clause specifying which single service you want to the the ranking for.

    I look forward to your resonse.
    Brian
  6. I'm working with the REPLACE command right now.

    Without using Replication, the best case solution is to restrict updates to one addressbook OR the other but not both.

    When updating the addressbook that was "read-only", you could use a process such as:

    1) Copy/dump the addressbook table to the target server as, say, sourceAddressBook.
    2) execute a query such as :
    [code]REPLACE INTO addressBook (ID, firstName, lastName, homeTel )
    SELECT ID, firstName, lastName, homeTel
      FROM  sourceAddressBook
    ON DUPLICATE KEY UPDATE
        ID = VALUES(ID),
        firstName = VALUES(firstName),
        lastName = VALUES(lastName),
        homeTel  = VALUES(homeTel);[/code]

    This assumes a primary (unique) key on ID.
    So, if any record changed, it would update that record but if a new record were created, it would be inserted.

    If you included a TIMESTAMP column  then you could limit the records copied from the modified database to those inserted/updated after a given TS.

    Brian
  7. I get the sense that the problem is not being fully described - execute
    [code]describe [i]tablename[/i][/code]
    queries for the relevant tables and describe the problem refrring to the queries' output.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.