I'm trying to create an Alpha Numeric List of links. Each alphabet links to a page that will display rows whose titles start with that alphabet or numeral.
For each alphabet, I want to get the number of titles that start with that alphabet. This allows me to link only the alphabets that actually have corresponding rows.
Here is the structure of the table:
CREATE TABLE IF NOT EXISTS `jos_mediaboss` (
`id` bigint(11) NOT NULL auto_increment,
`title` varchar(100) NOT NULL,
`catid` bigint(11) NOT NULL,
`published` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM ;
The query I'm doing is a LEFT JOIN repeated on the table for each alphabet.
SELECT count(ma.id) AS ca,
count(mb.id) AS cb,
count(mc.id) AS cc,
count(md.id) AS cd,
count(me.id) AS ce,
count(mf.id) AS cf,
count(mg.id) AS cg,
count(mh.id) AS ch,
count(mi.id) AS ci,
count(mj.id) AS cj,
count(mk.id) AS ck,
count(ml.id) AS cl,
count(mm.id) AS cm,
count(mn.id) AS cn,
count(mo.id) AS co,
count(mp.id) AS cp,
count(mq.id) AS cq,
count(mr.id) AS cr,
count(ms.id) AS cs,
count(mt.id) AS ct,
count(mu.id) AS cu,
count(mv.id) AS cv,
count(mw.id) AS cw,
count(mx.id) AS cx,
count(my.id) AS cy,
count(mz.id) AS cz
FROM jos_mediaboss AS m
LEFT JOIN jos_mediaboss AS ma ON (ma.title LIKE 'a%' AND ma.published = 1)
LEFT JOIN jos_mediaboss AS mb ON (mb.title LIKE 'b%' AND mb.published = 1)
LEFT JOIN jos_mediaboss AS mc ON (mc.title LIKE 'c%' AND mc.published = 1)
LEFT JOIN jos_mediaboss AS md ON (md.title LIKE 'd%' AND md.published = 1)
LEFT JOIN jos_mediaboss AS me ON (me.title LIKE 'e%' AND me.published = 1)
LEFT JOIN jos_mediaboss AS mf ON (mf.title LIKE 'f%' AND mf.published = 1)
LEFT JOIN jos_mediaboss AS mg ON (mg.title LIKE 'g%' AND mg.published = 1)
LEFT JOIN jos_mediaboss AS mh ON (mh.title LIKE 'h%' AND mh.published = 1)
LEFT JOIN jos_mediaboss AS mi ON (mi.title LIKE 'i%' AND mi.published = 1)
LEFT JOIN jos_mediaboss AS mj ON (mj.title LIKE 'j%' AND mj.published = 1)
LEFT JOIN jos_mediaboss AS mk ON (mk.title LIKE 'k%' AND mk.published = 1)
LEFT JOIN jos_mediaboss AS ml ON (ml.title LIKE 'l%' AND ml.published = 1)
LEFT JOIN jos_mediaboss AS mm ON (mm.title LIKE 'm%' AND mm.published = 1)
LEFT JOIN jos_mediaboss AS mn ON (mn.title LIKE 'n%' AND mn.published = 1)
LEFT JOIN jos_mediaboss AS mo ON (mo.title LIKE 'o%' AND mo.published = 1)
LEFT JOIN jos_mediaboss AS mp ON (mp.title LIKE 'p%' AND mp.published = 1)
LEFT JOIN jos_mediaboss AS mq ON (mq.title LIKE 'q%' AND mq.published = 1)
LEFT JOIN jos_mediaboss AS mr ON (mr.title LIKE 'r%' AND mr.published = 1)
LEFT JOIN jos_mediaboss AS ms ON (ms.title LIKE 's%' AND ms.published = 1)
LEFT JOIN jos_mediaboss AS mt ON (mt.title LIKE 't%' AND mt.published = 1)
LEFT JOIN jos_mediaboss AS mu ON (mu.title LIKE 'u%' AND mu.published = 1)
LEFT JOIN jos_mediaboss AS mv ON (mv.title LIKE 'v%' AND mv.published = 1)
LEFT JOIN jos_mediaboss AS mw ON (mw.title LIKE 'w%' AND mw.published = 1)
LEFT JOIN jos_mediaboss AS mx ON (mx.title LIKE 'x%' AND mx.published = 1)
LEFT JOIN jos_mediaboss AS my ON (my.title LIKE 'y%' AND my.published = 1)
LEFT JOIN jos_mediaboss AS mz ON (mz.title LIKE 'z%' AND mz.published = 1) LIMIT 1
This is query is generated dynamically.
It returns something like:
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| ca | cb | cc | cd | ce | cf | cg | ch | ci | cj | ck | cl | cm | cn | co | cp | cq | cr | cs | ct | cu | cv | cw | cx | cy | cz |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 84 | 0 | 0 | 0 | 0 | 0 | 84 | 84 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1 row in set (0.02 sec)
The problem is that I the counts for each row that starts with an alphabet is wrong. There are only 7 entries in the table, but the count() or each "id" returns 84 or some larger number. if the is at least one entry.
ie:
This is the count:
count(ma.id) AS ca,
for this join:
LEFT JOIN jos_mediaboss AS ma ON (ma.title LIKE 'a%' AND ma.published = 1)
or is it?
If there are rows starting with a, the returned column 'ca' will be 84 instead of the count just for that JOIN condition, as I want.