• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ken Shamrock
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • 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 ]
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least in Oracle you would say:

I am not sure about mysql
Please let us/me know.
 
Praveen Garimella
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic