Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Showing Query Result With Default Values When No Data Available


I have a scenario that user can ask for certain data say income & expense for last 3 months on monthly basis. I have wrote following query which works well but it has an issue:

Let say I have asked for data of FEB 2007, JAN 2007, DEC 2006 using the following query it shows me correct result when all three months have some data but it doesn't show me the desired result when you don't have data in one or more months.

Any suggestion to get result when there is no data:




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Default Values
I'm building a website, and a control panel along with it. The admins on the control panel will not all have full access. They get permission to do certain actions. These permissions are stored in the table crew_site_permissions.

The table crew_site_permissions has an index field (not auto_incremented) named crew_id. Whenever a new admin account is installed by one of the current admins, I want the site to automatically create the privileges. So the only thing I want to insert, is the new admin's ID. The rest of the fields (at least 30 others), should get their default value (1 or 0).

Default Values
sometimes you want to have a field in the database that sometimes contains data and sometimes it doesnt. For example, say you have a list of forums and some forum have passwords and some do not, i have set my password field to be a varchar(32) so it can store an md5 hash. But what shoulw i set the default value to. I have tried haveing a NULL field but also setting it as 0 (for NOT having a password)

Need Help - Mysql 5 Default Values
I created a program for mysql 3.23...
We launch on tuesday...
Problem is we are installing it on laptops and are tech guy installed mysql 5 instead of the version we currently use. In the mysql 3 when you make a query if there is a null value in the query it automatically places the default value into the table.
I.E.
Query: INSERT INTO someTable SET someCol=''
Column Default: 'a'
In mysql 5 it throws an error saying the column value is out of range if your trying to insert a null value. I need it to automatically insert the default value.
Is there a way to change the configuration of mysql to automatically insert the default value if a null value is encountered (and the column is a not null setting).
This is really really important,



Are Default Values Necessary/good?
Any thoughts on whether or not every column should have a default value? phpmyadmin seems to add them to everything, but I'm not sure if they are really necessary in a well structured database. All of the defaults end up being empty strings or 0 so php wont treat that any differently from a NULL value when the data is retrieved, will it?

2 examples:
with defaults:


CREATE TABLE address (
ID int(11) NOT NULL auto_increment,
Street_1 varchar(64) default '',
Street_2 varchar(64) default '',
Street_3 varchar(64) default '',
City varchar(64) default '',
State smallint(6) default Ɔ',
Country smallint(6) default Ɔ',
Zip varchar(8) default '',
PRIMARY KEY (ID)
) ENGINE=InnoDB;
without:

CREATE TABLE address (
ID int(11) auto_increment,
Street_1 varchar(64),
Street_2 varchar(64),
Street_3 varchar(64),
City varchar(64),
State smallint(6),
Country smallint(6),
Zip varchar(8),
PRIMARY KEY (ID)
) ENGINE=InnoDB;

Setting Default Values
I'm trying to set some tables in mysql and I was wondering if you could set default values for each of the columns and how.

Functions As Default Values
I am trying to use a function as a default value for a column but do not
seem to get the desired result. I want to use the NOW() function for a
last_updated column, here is my code...

CREATE TABLE test_table (
last_updated datetime NOT NULL default `NOW()`
) TYPE=MyISAM;

This gives an error;

CREATE TABLE test_table (
last_updated datetime NOT NULL default 'NOW()'
) TYPE=MyISAM;

Now the table shows a default value of 0000-00-00 00:00:00, when I add a new
row the value of last_updated is also 0000-00-00 00:00:00. I am using MySQL
3.23.37,

Default Values Not Working
I have a table which looks like this


/* Formatted on 2006/09/18 20:37 (QP5 v5.50) */
CREATE TABLE `user` (
`USER_ID` int(15) NOT NULL auto_increment,
`FIRST_NAME` varchar(255) default ' ',
`LAST_NAME` varchar(255) default ' ',
`ADDED_BY` int(15) default '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

There is a default value on the nullable column ADDED_BY. I explicitly issue a update sql to verity the default values are working

UPDATE USER SET ADDED_BY = NULL;

At the end of statement execution - I would expect the value in the table to be 0 but the database still shows null.

Default Field Values
I am trying to devise a procedure to delete rows wherein all the fields have
been set to their default values (either null, or in the case of a not null
default value for a row, that default value). Is there a means in MySQL
which allows me to obtain what the defaults are for all fields in a table?

How To Set Default Values For Timestamp Fields
what is the syntax for specifying the default values for timestamps in
MySQL in the CREATE TABLE command?

I have 2 timestamps, one for when the record was entered, the other
for any subsequent updates.

I know that the first timestamp column will be set automatically on
updates, but how does the other for creation timestamp get set.

My app is not generating the SQL directly so setting the value using
the NOW() command is not an option, as the inserts are coming from
different timezones I cannot let the clients set their own values.

INFILE Not Populating Default Values
windows 2000/MySql 4.1

I have a table that has a couple varchar fields set to default values.  When I run an insert statement the default value fields fill as expected.  When I use an INFILE statement to add data to the table the fields set to default values don't fill properly.  Any ideas how I can get the default values to populate using infile?

DATE / TIMESTAMP Default Values
im is having problems with auto inserting date in my table.

I dont know what is the default value i will set if column type to DATE. It was succesful though if is use the TIMESTAMP type in the column and is using CURRENT_TIMESTAMP as the dafault value. mysql automatically inserts the current date and time to the database everytime a new record is inserted. The problem with using TIMESTAMP is that i only want the date and not the additional time.

Is there a default value I can use for the DATE function instead of using TIMESTAMP? I already tried NOW(), CURDATE(), CURRENT_DATE() to no avail

How To Give 4 Default Values To A Column
I need to create a table in which a column is to be specified with 4 default values. Is it possible?

Set Default Values For Timestamp Fields
what is the syntax for specifying the default values for timestamps in
MySQL in the CREATE TABLE command?

I have 2 timestamps, one for when the record was entered, the other
for any subsequent updates.

I know that the first timestamp column will be set automatically on
updates, but how does the other for creation timestamp get set.

My app is not generating the SQL directly so setting the value using
the NOW() command is not an option, as the inserts are coming from
different timezones I cannot let the clients set their own values.

IN Query Showing All Records If Used With Same Field Twice
I have 2 tables programs and programType. programs has 5 records and programType has no records. When I run a query like "SELECT * FROM programs where id in (select programid from programType) and id in (select programid from programType)" I get all records But When I try to run "SELECT * FROM programs where id in (select programid from programType)" Then I get no records.

Showing Query Results In HTML
In mysql I can find the minimum data from a particular column by doing as follows:

SELECT MIN(Freq_Min) FROM datatable;

and mysql will display the results. Now I want to do the same thing but I want to do it in html, so I wrote the following code. When I open up the webpage stats.php it says that it couldn't execute the query. Can someone help get me on track? .....

How To Pass Default Values In A Stored Procedure
can anyone pls help me in passing default values in a stored procedure. That is if no value is passed to the SP then it should take the default value of that datatype.it is working in sql...but what is the alternative for that in mysql? my sql synatx will be something like this..

ALTER PROCEDURE [dbo].[usp_select_projectparticipants]
(
@ProjectId INT=NULL

what is the alternative in mysql?

Create Columns From Result Values
I've got a query running which displays a list of user actions and the number of users which have completed each action, grouped by their referral code. The output is along the following lines:
Count | Action | Referral
1 | View | Google
2 | Download | Yahoo
etc

However, I'd like to format the data with a separate column for each of the Referral results, and a row for each action and value against each referrer. The output would like like:
Action | Google | Yahoo
View | 1 | 0
Download | 0 | 2 .

Query Showing More Results In PHPMyAdmin Than In My Application
I am trying to run a query on my database, my database class returns 5 results, when I run it on PHPMyAdmin (direct copy), it returns 15 results. I am 100% sure I am copying the exact query. I tried my customer db class and ADODB.

I've tried many search queries, so it's not just this one.

MySQL: 4.0.27-standard
PHP: 4.47

Any ideas would be great appreciated, I am pulling my hair out......

Date/Time As A Default Values For A Table Column
I am new to mySQL, so this question might be simple.I want to add a default value to a column that is the current date/time. I am using the mySQL Administrator and will not allow me to use a function like CURRENT_TIMESTAMP() or NOW() as a default value. I used to do this with other databases (I always add a column to all of my tables called InsertDateTime and UpdateDateTime. It helps to track down data entry problems)

Simple Sql Question: Using A Query Result As A Query Variable
EDIT: it works now, I had an error in my code, not my method.

I have a very simple question.

I have 2 tables: 'users' and 'posts' with the following structure:

users: id, username, email_address
posts: id, user_id, post_title, post_text

in a my own mind's mysql, I would like to:

SELECT posts.id, posts.user_id, posts.post_title, posts.post_text users.username FROM users, posts WHERE posts.user_id = users.id

I usually do one query for the post data, and then, based on the use_id record, do another of the users table, but today, I'm being forced to do them in one swoop.

Adding Default Data
I have a column in my database which is a datefield... It has some data in it already, but for the cells that aren't filled in. If I edit this column to have a default value will it override the entire column? And also can I add 'unknown' into the column as the default? Or do I need to add something like 0000-00-00?

Data Type And Default Value
If I want to set a default value if a field to 5 paragraphs of text (+/- 1,400 characters) do I make it a TEXT field?
I want a user to be able to log in, edit the field and log out saving his changes to his record so that the revised text is available to him the next time he logs in.

Load Default Data
I've read that mysql's LOAD DATA INFILE is up to 20 times faster than an insert query. If I need to load a client's default configuration into a table, I could use only this query, but now i need to load the defaults and other identification data from php into some of the fields. Which method would be most efficient?Making a full insert query with the default data and the identification datausing LOAD DATA INFILE and then updating the identification fields with an UPDATE querywrite the default configuration data and the identification data to a file and then use LOAD DATA INFILEperhaps another better approach that I don't know of

Using A Join To Override Default Data
Lets say we have a single table (perhaps for language phrases)

Table: _language_phrases
phraseid
languageid
variablename
content

We want to select phrases for a certain language, but if that language phrase is not translated it will get the default language phrase.

In below example (-1) for languageid means its the default language phrase.

For instance if in the above table we have data:
1, -1, 'email', 'Email Address'
2, 1, 'email', 'English Translation of Email'
3, -1, 'username', 'User Name'

So lets say we want to get the language id 1 phrases selected but if there is a language phrase not translated for language 1 (such as 'username' in our example above) we need to get the default (-1) value for that phrase.

I thought you could do a left join on the 'variablename' column using the default language but im not really sure how to get a value for every phrase returned in the SELECt part of the query.

Default Value For The Datetime Data Type
I have a datetime data type in one of my tables and i want it to set the current date to itself when a record is inserted into the table. I know i could use the timestamp data type and assign CURRENT_TIMESTAMP as the default, but can this be done with the datetime data type as well?

I already have a field with the timestamp data type, it is used to determine when the record was last updated. I want another record to do basically the same thing except it must only contain when the record was added to the database. I tried using timestamp with its default value but apparantly i can't do that, navicat won't let me assign more than one timestamp field a default value of CURRENT_TIMESTAMP...

so i'm turning towards using the datetime data type to get this done, is there a default value i could use to allow the datetime data type to assign the current date and time to itself when a record is added to the table?

First Query Result Used For Second?
Is this possible / Practical?

Function1
I have a form that takes a users single field input and queries the database returning two values as result of what was entered, valueA and valueB. It then calls function2 passing it valueA and valueB.

Function2
takes valueA & valueB and queries the database again using these values within the query.

Im still learning mysql so the code is a real mess and not working.

Could someone give a simplified example of how this could / should be done?

#1101 - BLOB/TEXT Column 'data' Can't Have A Default Value
Im using phpmyadmin for working with mySQL. (its my local version, im just trying to use it).

When i want to add BLOB type column in table - that mistake appears:
#1101 - BLOB/TEXT column 'data' can't have a default value

I turned off strict mode (from my.ini, or by config wizard), but it did not help.

I've found alot about that problem in that site, but i dont uderstand anything, because im just Newbie.

So what should i do to make BLOB/TEXT column have a default value? Is it possible without using hard way, just by changing something, or download any new version?

MySQLerror: Data Source Name Not Found And No Default Driver Specified
using this connection string;

"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;PORT=3306;DATABASE=dbtest; USER=root;PASSWORD=admin;OPTION=3;"

i still encounter this message:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

my connection works on the server.. but it doesnt work in clients?

Default MySQL Return Value For Query
I'm writing a multilingual web site where not all content will necessarily be multilingual. I have a languageid and want to retrieve the content for that language. However, if that content doesn't exist, I would want to retrieve content for the default language.

My ideal query would be something like:

SELECT content FROM page_content WHERE pageid=$pid AND languageid=$userlang IF NO RESULTS WHERE pageid=$pid AND languageid=$defaultlangid

I imagine my made- up 'IF NO RESULTS' directive does not exist. Is there any way to get that kind of functionality from one query? Since I will be having queries all over the site to retrieve different kinds of localized things (image tags, html code, textual content), it would be awesome if I could figure out a way to consolidate.

Maybe there is a way to sort results from this query:

SELECT content FROM page_content where pageid=$pid AND (languageid=$userlang OR languageid=$defaultlang) ORDER BY .... ?

I'm also not sure if the nested boolean will work.

Default Database In Select Query
I have a (seemingly) simple query problem:
SELECT COLUMN FROM MYDB.MYTABLE works great.
SELECT column FROM mytable fails, because it trys to find the table 'mysql.mytable', which doesn't exist.

I am trying to migrate from Oracle to MySql, and I am using the toplink connector. With Oracle, I could do SELECTs without referencing the specific database, and it would work fine.

How To Query First 10 Result From Mysql
i am new to mysql/php , i want to query last 10 result from mysql database, can anyone help me with the command?


How To Get Sql Query Result Into A File
can someone suggest me way to get large mysql select query into file? Is there a mysql built in function to do this? I know sybase have bcp to do this.

Query In Storing The Result Set
SELECT ProductId FROM product WHERE prod_category_id IN (1,2) INTO OUTFILE '/usr/local/mysql/data/test/test.xml';

But what is my doubt is?

I want to save the output of the file in the remote server, is that possible?
http://mysql-api.blogspot.com/

Query Result As Column Name?
I now have made a view with results of several years for all the people in the database.

I can query the view to get the three winners, sorted decending on Winstperc. The result looks like this:

Seizoen    Naam        Winstperc
2006/07    Jim        1,0909
2006/07    Bill        1,0896
2006/07    William    1,0879

And that's exactly what I want to know but I need it like this:

Seizoen    Nr1        Nr2        Nr3
2006/07    Jim        Bill        William

Looking through the threads I got some nice ideas but...
I tried it with IF and with Case but never succeeded.

So, I think I'm overlooking something. Please show me the way?

Unexpected Query Result Using IN()
SELECT post_id, post_user, post_subject, post_category, post_date FROM sb_blogposts WHERE '10' IN(post_category)

The above query is only returning results if the 10 is the first ( or only ) category number in the post_category field.

post_category has been defined as a varchar(255) and contains a listing of categories that each post belongs in. All of the posts in this database have their categories set properly, and some posts are in more than one category.

The query example should be returning with every post that has "10" in the list. but for some reason if the category is less than 10, it won't show. It's a bit strange and I don't understand why it's doing that.

Query Result As New Table?
How can I store a Query Result as a new Table?

How To Repeat A Result Set From A Query?
I need to repeat any times a result from an query, like this:

select name, price, code
from `acme`.`table`

results:

chave de fenda 1pol 10.50 048929
chave de fenda 2pol 10.90 048927

Instead of this, I need 7 times the first product and 5 times the second one, like that:

chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 1pol 10.50 048929
chave de fenda 2pol 10.90 048927
chave de fenda 2pol 10.90 048927
chave de fenda 2pol 10.90 048927
chave de fenda 2pol 10.90 048927
chave de fenda 2pol 10.90 048927

Any idea?

Echo A Query Result
If I have a query:

$sql= mysql_query("SELECT a.id, b.id FROM table1 a, table2 b WHERE a.id=b.id");
$get= mysql_fetch_query($sql);

Normally, if the query is selecting just from one table, then I just use:
echo $get[id]

But how do you output both a's id and b's id?

I tried echo $get[a.id] but it doesnt work.

Strange Query Result
I have a column (tinyint) that has either 1s or 0s in it. When I run this query counting either 1s or 0s it counts all of the rows, not just the ones with 1s or 0s as stated in the query:
SELECT COUNT( cb_paiddues = '0' ) from jos_comprofiler
or
Code:

SELECT COUNT( cb_paiddues = '1' ) from jos_comprofile

Query Result Percentage
how I might be able to get each record's query match percentage? What I mean is, say your query contains multiple criteria, of which, a variable number of fields per row will match. You see this often with dating services, e.g. name, height, weight, hobby, etc. Not all fields will match so a percentage is provided, then you can sort by percentage. Is there a mySql function that can do this or must this be done the tedious way - like using PHP to test each value against each field's data per row, then updating, then sort

Output Query Result
Can I output a mysql query result direct to an Access database? I know (probably) I could use outfile to csv and read that through ODBC but then I've got the problem of not being able to overwrite it. I'm thinking that if I can output direct into an mdb it would get around that limitation.

How To Show All Records In MYSQL Query Using Default Value
I'm working on somesite and i face this problem:

i want to send a parameter entered by a user from text box that called (txt1) to the query below ....

Sub Query To Comma Separated Result?
I have a query that uses a subquery to return all of the color/size options a particular product has. I would like to get 1 set of result and just have the color/size options outputted to a comma separated result.

eg. result:
+---------------
size_colors
+---------------
small / white, medium / white, large / black

I thought I had done this before, but I can't find the code and I looked through my old threads on here and I couldn't find anything. Is this available in MySQL? Am I trying to do this in a really bad way? I'd like to avoid looping through my results and also avoid multiple queries.

I also need to do this with category names and a keyword table we have.

Display Result From Query In More Then 1 Column
I want to make a query to a database, and display it on my php page either through php code or with 2 queries to my mysql database.

This is how i want it displayed:

Data 1 Data 2
Data 3 Data 4
Data 5 Data 6
Data 7 Data 8
... ....
Data X Data X


This is my current code which i use and get it displayed in 1 row:

Copy The Result Of A Query Into Another Table.
I am trying to make a select with a sum in it and group by account and copy the results in another table.

As the site has the 4.0.25 MySQL version i guess i cant make subconsults. I thought of storying the results in vectors, but up to now it didnt work ...

Add The Result Of One Query To The Contents Of One Table
I want to add the result of one query to the contents of one table:

For instance, the query is:

select count(*) from names;

and the other table is called spanish_names

How To Ommit First Record In Query Result?
How can I ommit the first record of a Query?

Join Query Result Difference Between 3.23.49 And 4.0.13
I have what seems to me a very common operation i'm performing. I need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql v4.0.13-standard for pc-linux. There must be a better way to query for this information than the method i'm using, since the result with v4.0 is not what I expected, nor what I received with v3.23. I'm including sample data and queries with
my results.....

Query Result In A Specific Format
I need a query in which will give me a single column result
-----------------------------------
| [any column Name] |
----------------------------------
8:00 AM
----------------------------------
9:00 AM
----------------------------------
10:00 AM
-----------------------------------
11:00 AM
----------------------------------

I want the query to return a single column name with multiple rows in return ...


Copyright © 2005-08 www.BigResource.com, All rights reserved