|
-
May 6th, 2008, 04:49 AM
#1
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
-
May 6th, 2008, 05:23 AM
#2
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
-
May 6th, 2008, 06:59 AM
#3
Re: mysql helps
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
May 6th, 2008, 07:35 PM
#4
Re: mysql helps
 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'??
-
May 6th, 2008, 08:02 PM
#5
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
-
May 6th, 2008, 08:43 PM
#6
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.
-
May 6th, 2008, 11:11 PM
#7
Re: vb .net fill()
Post only one time, but search before you do
-
May 7th, 2008, 03:06 AM
#8
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
-
May 7th, 2008, 03:14 AM
#9
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
-
May 7th, 2008, 08:38 AM
#10
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
-
May 7th, 2008, 08:46 AM
#11
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
-
May 7th, 2008, 08:49 AM
#12
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
-
May 7th, 2008, 09:01 AM
#13
Re: vb .net fill()
Are these :
 Originally Posted by hsteo
table1: able, boy, cat
table2: able, donkey, eagle
Really your field names ¿
Not good design, if it is.
-
May 7th, 2008, 09:08 AM
#14
Re: mysql helps
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
May 7th, 2008, 09:09 AM
#15
Re: mysql helps
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|