TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-11-2008, 08:10 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default Need help with a query

Right now, I am using this query to grab all the email domain names from all my users:

SELECT DISTINCT REVERSE(LEFT(REVERSE(email),LOCATE('@',REVERSE(ema il)) - 1)) AS domain FROM users ORDER BY domain


Is there a way to alter this query where it will return 2 columns of data, 1 with the results from above and 1 with count of users with the domain?

Thanks in advance
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 01-11-2008, 08:50 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Um, that's what we call obfuscated code. That's really a wacky query, no offense meant. How about:
sql Code:
SELECT DISTINCT SUBSTRING(email, LOCATE('@', email)+1) AS domain FROM users ORDER BY domain

I'm not particularly good at SQL yet, so I'm not sure how you'd count users per domain. It could be group-by or a subquery as a variable name.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-11-2008, 09:03 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I'm don't believe there is a way to group things by a part of a value using mysql.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-11-2008, 09:31 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

The HAVING clause will allow you to use a value. You can also use variables in MySQL, like so:

sql Code:
SELECT @myVar:=COUNT(myColumn)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
webosb (01-11-2008)
Old 01-11-2008, 09:38 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

My version of MySQL (5.0.27) allows me to make the following query without any complaints:

SQL Code:
SELECT DISTINCT
    SUBSTRING(email, LOCATE('@', email) + 1) AS domain,
    COUNT(*) AS domain_count
FROM
    users
GROUP BY
    domain
ORDER BY
    domain
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
webosb (01-11-2008)
Old 01-11-2008, 10:12 PM   #6 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
My version of MySQL (5.0.27) allows me to make the following query without any complaints:

SQL Code:
SELECT DISTINCT
    SUBSTRING(email, LOCATE('@', email) + 1) AS domain,
    COUNT(*) AS domain_count
FROM
    users
GROUP BY
    domain
ORDER BY
    domain
This is exactly what I wanted. I'm still new to GROUP BY. Thanks alot Salathe!
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


All times are GMT. The time now is 05:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design