This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

[fixed] Urgent SQL question: Is it possible to do this?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two tables- table1 & table2

I want to join them with 127.0.0.3 SHOWN in NEW TABLE even it have no
hits (in table2)
mysql> select a.ip,name,time,sum(hits) from table1 a left join table2 b
on a.ip=
b.ip group by a.ip,a.name;

But if I specify the time ( > 2000 ) using where time>'2001' , I can't
get what my want (Also show 127.0.0.3)
What can the SQL do to do what I want? Many thanks!!
mysql> select a.ip,name,time,sum(hits) from table1 a left join table2 b
on a.ip=
b.ip where time>'2001' OR time=null group by a.ip,a.name;

pls read here if you cant see above:
http://www.hk3s.com/db.txt
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 18, 2003: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At least in Oracle you would say:

I am not sure about mysql
Please let us/me know.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please send the table descirptions i.e., using desc table1 and desc table2. And can we also know the mysql version you are using?
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to join them with 127.0.0.3 SHOWN in NEW TABLE even it have no
hits (in table2)
mysql> select a.ip,name,time,sum(hits) from table1 a left join table2 b
on a.ip=
b.ip group by a.ip,a.name;

But if I specify the time ( > 2000 ) using where time>'2001' , I can't
get what my want (Also show 127.0.0.3)
What can the SQL do to do what I want? Many thanks!!
mysql> select a.ip,name,time,sum(hits) from table1 a left join table2 b
on a.ip=
b.ip where time>'2001' OR time=null group by a.ip,a.name;

PS: Pls be remind that this all can be do in MSSQL but not MYSQL. Thanks
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 18, 2003: Message edited by: Dirk Schreckmann ]
 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made this one, seem to be work, need to test:
SELECT a.ip, name, SUM(hits)
FROM table1 a LEFT JOIN
table2 b ON a.ip = b.ip
WHERE time > '2001' OR
time = NULL
GROUP BY a.ip, a.name
UNION
SELECT c.ip, c.name, 0
FROM table1 c
WHERE NOT EXISTS
(SELECT 'X'
FROM table2 d
WHERE d .ip = c.ip)
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic