mysql - Mixing two tables together -


i have 2 tables

db2:

id, db1id, text, count_db1 

db1:

id, text 

db2 above created by

select *, count(`db1id`) count_db1 `db2` group `db1id` order count_db1 desc 

so last column added , whole output sorted descendingly count_db1.

db1ids ids of db1. want to

select * db1 

ordered value of count_db1 in db1. if id of db1 not existant in db2 db1id should added end of list(ie assigned value of 0 count_db1).

example:

db2: id, db1id, text, count_db1 1,4,hello,5 2,4,hello,5 3,4,ho,5 5,4,yeah,5 6,4,no,5 4,3,no,1  db1: id, text 3, yeahright 4, whatever 

so in db2 db1id 4 occurs 5 times, db1id 3 occurs 1 time. order entries of db1 such id 4 comes before id 3. result should be:

  1. whatever
  2. yeahright

a simple left join count should want;

select db1.* db1 left join db2   on db1.id=db2.db1id group db1.id, db1.text order count(db2.id) desc 

an sqlfiddle test with.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -