11 June 2012

SQL query : count and result in MySQL

SQL query : count and result one query in MySQL Table: banks

bank_id bank_name bank_abbrev
1 Bangkok BBL
2 Kasikornthai KBANK
3 Siam Commercial SCB
4 Krungthai KTB
5 Krungsri BAY
SQL query:
SELECT b.*, t.total
FROM banks b
LEFT JOIN (
  SELECT count(*) AS total
  FROM banks
) AS t ON b.bank_id != -1
LIMIT 0, 3
Query results:
bank_id bank_name bank_abbrev total
1 Bangkok BBL 5
2 Kasikornthai KBANK 5
3 Siam Commercial SCB 5

1 comments:

Unknown said...

ผมลืมไปแล้ว sql เขียนยังไง

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Blog by Chagridsada Boonthus | http://chagridsada.blogspot.com/