Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
SQL: selecting entries starting with a certain letter
Old 03-20-2009, 04:00 PM SQL: selecting entries starting with a certain letter
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
On a page I have links, in the form of banners, to other sites, and they have mine. They're all stored in a database and there is no problem displaying them. However, there's starting to get rather many of them and I want people to be able to view only a few of them at a time by selecting a letter, and then only be shown those of which title starts with that letter.

How could I write a sql query to select and count the number of titles beginning with each letter? That is, if there is 3 titles starting with A, none with B, 1 with C and 5 with D, how would I write to select A-3, C-1, D-5?

And what do you think would be best, to only display the letters A, C and D (as links) or to display all letters but not making links out of the other ones?

Thanks
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
 
Register now for full access!
Old 03-20-2009, 04:06 PM Re: SQL: selecting entries starting with a certain letter
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
SELECT COUNT(column) AS rows,fieldlist FROM table WHERE column LIKE 'letter%";
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-20-2009, 04:31 PM Re: SQL: selecting entries starting with a certain letter
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
That's not really what I was looking for I think. I want to select all the (first) letters from the title, and how many times they occur. That is, before I have clicked and chosen a letter. I want to let people know how many entries there are for each letter.

Lets say I have this table
Code:
title   other fields...
------------------------
A1
A2
A3
C1
D1
D2
D3
D4
D5
------------------------
I basically want an array containing

PHP Code:
array (
   
'A' => 2,
   
'C' => 1,
   
'D' => 5

So that I can display the links, as in
<a href="links.php?show=A">A (2)</a>
<a href="links.php?show=C">C (1)</a>
<a href="links.php?show=D">D (5)</a>
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-20-2009, 04:57 PM Re: SQL: selecting entries starting with a certain letter
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
'%' in SQL is a wildcard so "column LIKE 'a%'" will return all records where the column starts with a letter 'a' or 'A'

COUNT(column) will return the number of rows that have matched the WHERE clause.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-20-2009, 08:36 PM Re: SQL: selecting entries starting with a certain letter
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Yes, I know. So you mean I should run a query like

SELECT COUNT(*) as rows FROM table WHERE title LIKE '$letter%'

and run it for all letters a-z?

I just thought it might be possible to select the first letter, maybe by using a substr-like function, and maybe group by(?) that letter, and somehow also get the number of times that letter occurs. So that I can get the info by running a single query.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-20-2009, 09:11 PM Re: SQL: selecting entries starting with a certain letter
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you mean like
Code:
SELECT COUNT(column) as rows,left(column,1) AS letter FROM table GROUP BY left(column,1) ORDER BY left(column,1);
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-21-2009, 12:16 PM Re: SQL: selecting entries starting with a certain letter
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Exactly! That did it, gave me just what I wanted.
I used
SELECT COUNT(title) as count, left(title,1) as letter FROM links GROUP BY letter ORDER BY title

and it gave me
Code:
count | title
------------
  2   |  A
  1   |  C
  5   |  D
------------
Thanks chrishirst
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366

Last edited by lizciz; 03-21-2009 at 12:24 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to SQL: selecting entries starting with a certain letter
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.93286 seconds with 12 queries