How to: IFNULL(SELECT ...,0)


 
Thread Tools Search this Thread
Top Forums Web Development How to: IFNULL(SELECT ...,0)
# 1  
Old 10-22-2010
How to: IFNULL(SELECT ...,0)

Hi all,

Wondering is this possible:

SELECT id AS cid, name, phone, url, streettype, IFNULL((SELECT ROUND(AVG(stars)*2, 1) / 2 FROM " . $dbconf{'prefix'} . "reviews WHERE location=cid) AS avgstars,0), ...

OR should it be:

SELECT id AS cid, name, phone, url, streettype, (SELECT ROUND(AVG(stars)*2, 1) / 2 FROM " . $dbconf{'prefix'} . "reviews WHERE location=cid) AS IFNULL(avgstars,0), ...

I am fairly sure you can see what I am trying to achieve here.

In other words, two tables, one with id's and another that relates those ID's to star ratings.
I want to extract the AVG(stars) rating AS avgstars to operate on it later, but, if there is no results for the query: ie. no row exists with the "location" specified, can I make the return "avgstars" equal zero rather than simply NULL.

Thanks,
Aaron.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select command

Hi I'm using the "select" command in the global_env.sh to log in to the application directory. This file is called in .bashrc profile. Sample code: Filename: global_env.sh set -o vi export severname=$(uname -n) printf '%s\n%30s\n%s\n' "***********************" "Welcome to $severname"... (6 Replies)
Discussion started by: cheers799
6 Replies

2. Programming

Socket and select

I have created two sockets and binded both. My requirement is that 2nd socket must send/ recv data only on expiration of timeval(tv). but the 1st socket must keep on send/recv the data without waiting for the 2nd socket completion...... I have posted my code below...... In this code the 2nd... (3 Replies)
Discussion started by: naresh046
3 Replies

3. Boot Loaders

Reboot and Select Proper Boot device or insert Boot media in select Boot device and press a key

Hello, I have kubuntu on my laptop and now I decided to switch to Windows 7. I made the bios settings properly (first choice is boot from cd\vd) but I see the error " reboot and select proper Boot device or insert Boot media in select Boot device and press a key " I have tried CD and... (0 Replies)
Discussion started by: rpf
0 Replies

4. Programming

Problems with select

Is select only supposed to report state changes on an FD's, state, whether or not it had pending input available in the first place? I've got a situation where select() repeatedly reports no FD's ready for stdin when there's lots of data available. And if it only reports changes, how is this... (2 Replies)
Discussion started by: Corona688
2 Replies

5. Shell Programming and Scripting

Select everything before a pattern

Hi I have i doubt, actually i have to select everything before a word(pattern).For that i am using sed i am using the below line of code but it is not working i am getting a blank instead.. sed -n '/regexp/{g;1!p;};h' file1 Can anyone help? Thanks (15 Replies)
Discussion started by: usha rao
15 Replies

6. Shell Programming and Scripting

select a column

I've a file like this: andrea andre@lol.com october antonio@lol.com marco 45247@pop.com kk@pop.com may pollo@lol.com mary mary@lol.com can I select only the column with email adress? can I utilise a filter with @ ? I want obtain this: ... (2 Replies)
Discussion started by: alfreale
2 Replies

7. UNIX for Advanced & Expert Users

Doubt regarding Select()

Please provide the solution for the following scenario: 1) There are two process named as ProcessA and ProcessB 2) ProcessA has opend a named pipe in read mode.This has been made as blocking mode. 3) ProcessB opens this pipe and writes in Blocking mode.So wat happens is even if Process A goes... (2 Replies)
Discussion started by: sunil_ktg
2 Replies

8. Shell Programming and Scripting

How to select Shell

We are using the line #!/usr/bin/ksh in the script, to run the script in ksh. If I have Bash shell and Ksh, how to select the shell to run the script during runtime? Thanks in advace Victor (2 Replies)
Discussion started by: mvictorvijayan
2 Replies

9. IP Networking

select vs poll

Hi, Off late I had been looking at the differences b/w select() & poll() system calls. The requirement is to reduce the overhead, processor power in waiting for the data. In the kind of connections under consideration there would be very frequent data arriving on the sockets, so poll() fares... (12 Replies)
Discussion started by: smanu
12 Replies

10. UNIX for Dummies Questions & Answers

Select ALL in VI Editor

Hi all, How can i select all and copy text in VI Editor. Rakesh Gupta (22 Replies)
Discussion started by: rakish
22 Replies
Login or Register to Ask a Question
DBIx::Class::SQLMaker::LimitDialects(3) 		User Contributed Perl Documentation		   DBIx::Class::SQLMaker::LimitDialects(3)

NAME
DBIx::Class::SQLMaker::LimitDialects - SQL::Abstract::Limit-like functionality for DBIx::Class::SQLMaker DESCRIPTION
This module replicates a lot of the functionality originally found in SQL::Abstract::Limit. While simple limits would work as-is, the more complex dialects that require e.g. subqueries could not be reliably implemented without taking full advantage of the metadata locked within DBIx::Class::ResultSource classes. After reimplementation of close to 80% of the SQL::Abstract::Limit functionality it was deemed more practical to simply make an independent DBIx::Class-specific limit-dialect provider. SQL LIMIT DIALECTS
Note that the actual implementations listed below never use "*" literally. Instead proper re-aliasing of selectors and order criteria is done, so that the limit dialect are safe to use on joined resultsets with clashing column names. Currently the provided dialects are: LimitOffset SELECT ... LIMIT $limit OFFSET $offset Supported by PostgreSQL and SQLite LimitXY SELECT ... LIMIT $offset $limit Supported by MySQL and any SQL::Statement based DBD RowNumberOver SELECT * FROM ( SELECT *, ROW_NUMBER() OVER( ORDER BY ... ) AS RNO__ROW__INDEX FROM ( SELECT ... ) ) WHERE RNO__ROW__INDEX BETWEEN ($offset+1) AND ($limit+$offset) ANSI standard Limit/Offset implementation. Supported by DB2 and MSSQL >= 2005. SkipFirst SELECT SKIP $offset FIRST $limit * FROM ... Suported by Informix, almost like LimitOffset. According to SQL::Abstract::Limit "... SKIP $offset LIMIT $limit ..." is also supported. FirstSkip SELECT FIRST $limit SKIP $offset * FROM ... Supported by Firebird/Interbase, reverse of SkipFirst. According to SQL::Abstract::Limit "... ROWS $limit TO $offset ..." is also supported. RowNum Depending on the resultset attributes one of: SELECT * FROM ( SELECT *, ROWNUM rownum__index FROM ( SELECT ... ) WHERE ROWNUM <= ($limit+$offset) ) WHERE rownum__index >= ($offset+1) or SELECT * FROM ( SELECT *, ROWNUM rownum__index FROM ( SELECT ... ) ) WHERE rownum__index BETWEEN ($offset+1) AND ($limit+$offset) or SELECT * FROM ( SELECT ... ) WHERE ROWNUM <= ($limit+1) Supported by Oracle. Top SELECT * FROM SELECT TOP $limit FROM ( SELECT TOP $limit FROM ( SELECT TOP ($limit+$offset) ... ) ORDER BY $reversed_original_order ) ORDER BY $original_order Unreliable Top-based implementation, supported by MSSQL < 2005. CAVEAT Due to its implementation, this limit dialect returns incorrect results when $limit+$offset > total amount of rows in the resultset. FetchFirst SELECT * FROM ( SELECT * FROM ( SELECT * FROM ( SELECT * FROM ... ) ORDER BY $reversed_original_order FETCH FIRST $limit ROWS ONLY ) ORDER BY $original_order FETCH FIRST $limit ROWS ONLY ) Unreliable FetchFirst-based implementation, supported by IBM DB2 <= V5R3. CAVEAT Due to its implementation, this limit dialect returns incorrect results when $limit+$offset > total amount of rows in the resultset. GenericSubQ SELECT * FROM ( SELECT ... ) WHERE ( SELECT COUNT(*) FROM $original_table cnt WHERE cnt.id < $original_table.id ) BETWEEN $offset AND ($offset+$rows-1) This is the most evil limit "dialect" (more of a hack) for really stupid databases. It works by ordering the set by some unique column, and calculating the amount of rows that have a less-er value (thus emulating a "RowNum"-like index). Of course this implies the set can only be ordered by a single unique column. Also note that this technique can be and often is excruciatingly slow. You may have much better luck using "software_limit" in DBIx::Class::ResultSet instead. Currently used by Sybase ASE, due to lack of any other option. AUTHORS
See "CONTRIBUTORS" in DBIx::Class. LICENSE
You may distribute this code under the same terms as Perl itself. perl v5.18.2 2014-01-22 DBIx::Class::SQLMaker::LimitDialects(3)