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 05-03-2009, 09:43 AM   #1 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Big Grin MySQL query help

Hello again,

I'm working on this file upload tinyurl like thing and have some problems with one mysql query. There are 2 tables with "id"s and I want to count all data and views in those tables with a mysql query. But when there are no records for the specified user_id in one of the tables the views is set to NULL and I want an integer. I've looked at CAST and CONVERT but can't seem to get it to work.

This is my current query:
Code:
SELECT datax + datay AS data, viewsx + viewsy as views FROM
(SELECT COUNT(*) AS datax, SUM(views) as viewsx FROM `data_id_custom` WHERE `user_id` = 1) as x,
(SELECT COUNT(*) AS datay, SUM(views) as viewsy FROM `data_id` WHERE `user_id` = 1) as y
Hope this is enough information to get some help

sjaq
sjaq is offline  
Reply With Quote
Old 05-03-2009, 02:53 PM   #2 (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 way to solve this is to modify the database. For the views column, set it so it cannot be NULL, but also the default value of that column is 0.

That way you will have no NULLs in your database to mess up the counting, instead it will be populated with zeroes where there are no views.
__________________
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:
sjaq (05-03-2009)
Old 05-03-2009, 08:02 PM   #3 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

The views row already defaults to 0. The problem is when one of the SELECTS has no results e.g. there are no items in data_id_custom for user_id 12 so SUM(views) can't count any data.

This is the structure for both tables:

Last edited by sjaq : 05-03-2009 at 08:07 PM. Reason: smaller screenshot
sjaq is offline  
Reply With Quote
Old 05-03-2009, 08:44 PM   #4 (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

Perhaps look into using the COALESCE function in your query.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
sjaq (05-04-2009)
Old 05-04-2009, 10:33 AM   #5 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Thank you Salathe, this works:
Code:
SELECT datax + datay AS data, viewsx + viewsy as views FROM
(SELECT COUNT(*) AS datax, COALESCE(SUM(views), 0) as viewsx FROM `data_id_custom` WHERE `user_id` = 1) as x,
(SELECT COUNT(*) AS datay, COALESCE(SUM(views), 0) as viewsy FROM `data_id` WHERE `user_id` = 1) as y
sjaq 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql query johnN MySQL & Databases 6 04-05-2008 08:12 PM
Ending MySQL Query Execution? Orc General 2 02-07-2008 08:00 PM
MySQL Query; Counting and adding values ReSpawN Absolute Beginners 3 12-11-2007 10:07 PM
Rookietime : mySQL Query class ReSpawN Advanced PHP Programming 9 12-05-2007 06:04 AM


All times are GMT. The time now is 08:20 AM.

 
     

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