Welcome to Dream.In.Code
Become an Expert!

Join 150,381 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,327 people online right now. Registration is fast and FREE... Join Now!




MySQL problem

 
Closed TopicStart new topic

MySQL problem

edu2004eu
9 Sep, 2008 - 11:10 AM
Post #1

New D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 45


Dream Kudos: 25
My Contributions
Hi. I have a big MySQL problem. Here is an example of what I have in my DB:

table1:
  • Name | Age
    ========
  • Mom | 44
  • Dad | 42
  • Bro | 13
table2:
  • Name | Wish
    ========
  • Mom | Fridge
  • Mom | TV
  • Bro | Computer
I have the following code:
CODE
SELECT * FROM table1 AS t1, table2 AS t2 WHERE t1.name=t2.name


Problem is, MySQL brings back Mom 2 times, and I want him to show it only one time (doesn't matter which wish it shows). I have tried limiting it to 1 record, but then Bro doesn't come up. I have tried messing around with DISTINCT, but it wouldn't work. I have tried something with LEFT JOIN, but still nothing.
You guys are my last hope. Could you help me? Thank you.
User is offlineProfile CardPM
+Quote Post

capty99
RE: MySQL Problem
9 Sep, 2008 - 11:25 AM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
you could just split it up.

so you select the distinct from table 1,

then when your printing your stuff, in a loop do another query to find the info in table 2 that corresponds to the name from table 1.

probably not great for a huge db but for this it shouldn't be a problem.

good coding practice to definitely change names of tables beyond table 1 and 2.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: MySQL Problem
9 Sep, 2008 - 11:26 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
This
CODE

SELECT *
   FROM table1 AS t1
      left outer join table2 AS t2
         on t1.name=t2.name


Will get you all of t1 and matches, so
CODE

Mom | 44 | Fridge
Mom | 44 | TV
Dad | 42 |  NULL
Bro | 13 |  Computer


If you don't want two Mom, then you need to limit table2 to one entry per name, like so:
CODE

SELECT Name, min(Wish) as FirstWish
   FROM table2
   group by Name


If you want the join with those results, it's a sub query:
CODE

SELECT *
   FROM table1 AS t1
      left outer join ( SELECT Name, min(Wish) as FirstWish FROM table2 group by Name ) AS t2
         on t1.name=t2.name


Hope this helps.

User is offlineProfile CardPM
+Quote Post

edu2004eu
RE: MySQL Problem
9 Sep, 2008 - 11:29 AM
Post #4

New D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 45


Dream Kudos: 25
My Contributions
QUOTE(capty99 @ 9 Sep, 2008 - 12:25 PM) *

you could just split it up.

so you select the distinct from table 1,

then when your printing your stuff, in a loop do another query to find the info in table 2 that corresponds to the name from table 1.

probably not great for a huge db but for this it shouldn't be a problem.

good coding practice to definitely change names of tables beyond table 1 and 2.


That's what I was trying to do, but it kind of didn't work, it's too complicated.
And table1 and table2 were only examples tongue.gif

QUOTE(baavgai @ 9 Sep, 2008 - 12:26 PM) *

This
CODE

SELECT *
   FROM table1 AS t1
      left outer join table2 AS t2
         on t1.name=t2.name


Will get you all of t1 and matches, so
CODE

Mom | 44 | Fridge
Mom | 44 | TV
Dad | 42 |  NULL
Bro | 13 |  Computer


If you don't want two Mom, then you need to limit table2 to one entry per name, like so:
CODE

SELECT Name, min(Wish) as FirstWish
   FROM table2
   group by Name


If you want the join with those results, it's a sub query:
CODE

SELECT *
   FROM table1 AS t1
      left outer join ( SELECT Name, min(Wish) as FirstWish FROM table2 group by Name ) AS t2
         on t1.name=t2.name


Hope this helps.


Hmm... this might just work, I will try it wink2.gif Thank you.
User is offlineProfile CardPM
+Quote Post

edu2004eu
RE: MySQL Problem
10 Sep, 2008 - 08:12 AM
Post #5

New D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 45


Dream Kudos: 25
My Contributions
Yes, it worked. Thank you, baavgai for your help smile.gif

EDIT: but one last thing: if I want to let's say add a criteria to search only the wishes that start with C, where do I put the WHERE statement? I saw this in your code: on t1.name=t2.name so I added my code after that: AND t1.name LIKE '%C%' but that didn't work, it just showed me the results more distanced from each other... am I missing something?

Note: this is not the actual code I am working with so the LIKE thing may not have the right syntax, but it's only an example.

EDIT: nevermind, I found it biggrin.gif I had to put the WHERE statement after on t1.name=t2.name

This post has been edited by edu2004eu: 10 Sep, 2008 - 08:35 AM
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 03:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month