CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: mysql helps

  1. #1
    Join Date
    Jul 2007
    Posts
    116

    mysql helps

    I intending to join to 2 tables based on 2 conditions:

    Assume that
    table 1 has rows: Able, Bravo, Cam
    table 2 has rows: Able, Donkey, Eagle

    Mysql code is:

    SELECT table1.bravo, table1.cam left join table2 on table1.able = table2.able where donkey = 1

    But i have another condition and that is able must be of certain value. how do i code for that?

    thanx

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: mysql helps

    Hi hsteo...

    By the look of your query, you are taking all rows from table 1.. joining table 2 where the able column matches (left joined). Then you have a where condition on table2.donkey = 1.

    I would put these inside an inner join.. i.e:
    SELECT table1.bravo, table1.cam
    FROM table1
    INNER JOIN table2 ON table1.able = table2.able
    AND table2.donkey = 1
    AND able = ?

    ps: this should be in the database forum,

    best of luck
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: mysql helps

    [ moved ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Jul 2007
    Posts
    116

    Re: mysql helps

    Quote Originally Posted by HairyMonkeyMan
    Hi hsteo...

    By the look of your query, you are taking all rows from table 1.. joining table 2 where the able column matches (left joined). Then you have a where condition on table2.donkey = 1.

    I would put these inside an inner join.. i.e:
    SELECT table1.bravo, table1.cam
    FROM table1
    INNER JOIN table2 ON table1.able = table2.able
    AND table2.donkey = 1
    AND able = ?

    ps: this should be in the database forum,

    best of luck
    should it be a 'WHERE'??

  5. #5
    Join Date
    Jul 2007
    Posts
    116

    Problem solved due to carelessness

    hi,

    currently i am working on a database using vb .net and met with an error

    i have 2 tables:
    table1: able, boy, cat
    table2: able, donkey, eagle

    now i wan to extract eagle from table2 using table1.able

    so my code is as follows

    SELECT table2.eagle
    FROM table2
    INNER JOIN table1 ON table1.able = table2.able
    WHERE table2.donkey = 2
    AND table1.able = 2

    and my latter code is here

    Dim da As SqlDataAdapter = New SqlDataAdapter(sql, sqlConn)
    da.Fill(ds, "getCleaningChargeList") ' getCleaningChargeList is a virtual table

    However the code always throw me an error at fill statement. Any idea why?

    Thanx
    Last edited by hsteo; May 7th, 2008 at 04:26 AM. Reason: Solved

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: mysql helps

    Yes. I am sure it was just a typo.

    Code:
    SELECT table1.bravo, table1.cam FROM table1 INNER JOIN table2 ON table1.able = table2.able WHERE table2.donkey = 1AND able = ?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: vb .net fill()

    Post only one time, but search before you do
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: mysql helps

    You can do it in the where as PeejAvery described or in multiple join conditions. Results of the query should be similar... if your interested in performance, use the 'EXPLAIN' statement to guage the speed of execution (try each approach).
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  9. #9
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: vb .net fill()

    Lets see the select code from your data adapter. What is the exact error message?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  10. #10
    Join Date
    Jul 2007
    Posts
    116

    Re: mysql helps

    Hi thanx for the superby helpful hints and reply.

    In order not to spam the forum, here is another problem:

    As usual take 2 tables for example:

    table1:
    able 21/01/03
    bravo 01/10/07
    cat 03/09/07
    able 09/05.05
    bravo 07/06/06

    ok so now i would like to have the following result:

    able 09/05/05
    bravo 01/10/07
    cat 03/09/07

    Seriously i thought of using TOP in my query but couldn't display the rest of the list out. Any suggestion?

    Thanx

  11. #11
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: mysql helps

    top doesn't work in mysql.... its only for msaccess (as far as i know).

    You can use 'LIMIT'. To limit to the first 3 rows, your query might look like this:

    Code:
    SELECT table1.bravo, table1.cam 
    FROM table1 
    INNER JOIN table2 
    ON table1.able = table2.able 
    AND table2.donkey = 1 
    AND able = ?
    LIMIT 3
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  12. #12
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: mysql helps

    I might have that back-to-front a little, since you have your data in fields..

    Have a play around with 'LIMIT' and let me know how you get on
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: vb .net fill()

    Are these :
    Quote Originally Posted by hsteo
    table1: able, boy, cat
    table2: able, donkey, eagle
    Really your field names ¿
    Not good design, if it is.

  14. #14
    Join Date
    May 2002
    Posts
    10,943

    Re: mysql helps

    [ merged ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  15. #15
    Join Date
    Jul 2007
    Posts
    116

    Re: mysql helps

    Quote Originally Posted by HairyMonkeyMan
    top doesn't work in mysql.... its only for msaccess (as far as i know).

    You can use 'LIMIT'. To limit to the first 3 rows, your query might look like this:

    Code:
    SELECT table1.bravo, table1.cam 
    FROM table1 
    INNER JOIN table2 
    ON table1.able = table2.able 
    AND table2.donkey = 1 
    AND able = ?
    LIMIT 3

    oops guess i was misleading in the first place, anyway now I am trying to use just 1 table with the following data values

    able 21/03/08
    bravo 12/02/05
    charlie 13/09/06
    delta 23/04/06
    able 15/10/05
    delta 10/10/05

    if for example i want to look up for data that occurs between 1/11/05 - 1/05/06

    then the result must be as follows:

    able 15/10/05
    delta 23/04/06

    HTH thanx

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured