hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Simple SQL query needs a good home
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Simple SQL query needs a good home

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 11-24-2002, 11:01 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427

Simple SQL query needs a good home


select count(priority_id=1), count(priority_id=2),count(priority_id=3) from leads where account_id =3

I want to have a query to return 3 fields within 1 record. I have leads and I want to be able to a row[0] tell me how many priority_id=1, row[1] tell me how many priority_id=2, and so on

This query doesn't work and I wanted to see what your guys thought out there for a simple way to get this info without doing 3 different queries.

John

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Reply With Quote


Sponsored Links
  #2  
Old 11-25-2002, 02:09 AM
MarkIL MarkIL is offline
Aspiring Evangelist
 
Join Date: Jul 2002
Location: Israel
Posts: 351
SELECT COUNT(priority_id=1) AS p1, COUNT(priority_id=2) AS p2,COUNT(priority_id=3) AS p3 FROM leads WHERE account_id =3

and then just mysql_fetch_assoc().

__________________
C#/C++/TCL/Python/PHP developer.
mark at mark org il

Reply With Quote
  #3  
Old 11-25-2002, 09:39 AM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
Lovely, This will be great.

Thank you

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Reply With Quote
Sponsored Links
  #4  
Old 11-25-2002, 02:11 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
it didn't work correctly?

I got the same number for all three fields and I know they are different?? Weird I will have to look into this more later today

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Reply With Quote
  #5  
Old 11-25-2002, 02:27 PM
ScottD ScottD is offline
Web Hosting Master
 
Join Date: Dec 2001
Location: Detroit, MI
Posts: 1,067
Quote:
SELECT priority_id, count(*) FROM leads GROUP BY 1 ORDER BY 1
Should return a set of rows where the first column is the priority_id and the second column is the count. If you only want priority_id's 1, 2, and 3 then do this:
Quote:
SELECT priority_id, count(*) FROM leads WHERE priority_id in (1, 2, 3) GROUP BY 1 ORDER BY 1

__________________
<!-- boo! -->

Reply With Quote
  #6  
Old 11-25-2002, 02:41 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
that works better, what about it returning the name of the priority, So I can have as many priorities as I want. All i have to do is check the mysql_num_rows function and do a loop

#
# Table structure for table 'leads'
#

CREATE TABLE `leads` (
`leads_id` int(10) unsigned NOT NULL auto_increment,
`leads_firstname` varchar(30) NOT NULL default '',
`leads_lastname` varchar(30) NOT NULL default '',
`leads_company` varchar(100) NOT NULL default '',
`leads_spouse` varchar(30) NOT NULL default '',
`leads_guestofhonor` varchar(30) NOT NULL default '',
`leads_address` varchar(75) NOT NULL default '',
`leads_address2` varchar(50) NOT NULL default '',
`leads_city` varchar(50) NOT NULL default '',
`leads_state` char(2) NOT NULL default '',
`leads_zip` varchar(10) NOT NULL default '',
`leads_homephone` varchar(10) NOT NULL default '',
`leads_workphone` varchar(10) NOT NULL default '',
`leads_workphoneext` varchar(5) NOT NULL default '',
`leads_cell` varchar(10) NOT NULL default '',
`leads_otherphone` varchar(10) NOT NULL default '',
`leads_otherphoneext` varchar(5) NOT NULL default '',
`leads_fax` varchar(10) NOT NULL default '',
`leads_email` varchar(128) NOT NULL default '',
`leads_comments` text NOT NULL,
`leads_datecall` date NOT NULL default '0000-00-00',
`leads_dateevent` date NOT NULL default '0000-00-00',
`eventtype_id` int(10) unsigned NOT NULL default '0',
`account_id` int(10) unsigned NOT NULL default '0',
`location_id` int(10) unsigned NOT NULL default '0',
`referraltype_id` int(10) unsigned NOT NULL default '0',
`leads_budget` decimal(8,2) NOT NULL default '000000.00',
`leads_client` int(1) NOT NULL default '0',
`priority_id` tinyint(3) unsigned zerofill NOT NULL default '001',
PRIMARY KEY (`leads_id`),
UNIQUE KEY `leads_id` (`leads_id`,`leads_email`),
KEY `leads_id_2` (`leads_id`)
) TYPE=MyISAM COMMENT='All leads first go here';



#
# Table structure for table 'priority'
#

CREATE TABLE `priority` (
`priority_id` tinyint(3) unsigned NOT NULL auto_increment,
`priority_name` char(30) NOT NULL default '',
`priority_value` tinyint(1) unsigned NOT NULL default '1',
PRIMARY KEY (`priority_id`),
UNIQUE KEY `priority_id` (`priority_id`),
KEY `priority_id_2` (`priority_id`)
) TYPE=MyISAM;

This would be the 2 tables I want to reference. basicly in the first field I would want to name of the priority, the numbe rof leads that have this priority, and then the level so I can order them. from highest to lowest.

Thanks again for your help

John

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Alert Logic Releases New Version of Security and Compliance Solution Web Hosting News 2013-02-19 15:55:16
Video Demo: Yola Website Building Software Whir Tv 2012-05-14 16:05:42
Namesco Provides Free Domains for Companies Made Simple Customers Web Hosting News 2012-01-31 13:36:44
Web Host 1&1 Launches Domain Registration Mobile App Web Hosting News 2011-11-02 21:05:55
Hosting M&A Market Outlook with Peter Hopper of DH Capital Web Hosting News 2011-09-28 21:35:21


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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 Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?