I have prices in a database as INT if I order by I get
97,650
ends of appearing before
975,455
after sorting in DESC
It appears that since the 6 in the first price is greater than the 3rd place position in the second price, it counts that as a larger number.
Has anyone run into this problem and know how to solve it?
To test, try this slow and sad query.
select prices from (table) order by (prices+0);
If that query works, then it is likely your column is not an INT column. The (prices+0) makes certain that the data is treated as an integer.
Once you have found that this does indeed work for you, you need to alter the column to be an INT column.
You should change the field type from varchar to int.
Related
I want to know whether there are any '0's in the issues_status column of the issues table. The issues_status column store values: '0' & '1'. I guess a query find the count of 0's would give me the required result. May i know how to accomplish this.
issues Table
issues_id
issues_status
1
1
2
0
SELECT COUNT(issues_status)
FROM issues
WHERE issues_status=0
I want to fetch the total bill amount of particular day, but in my database all the amount are inserted with "£" symbol. Now I want to sum all the column values.can anyone help me to find out
For example:
I have created a table called order and the columns are id, totamt. In totamt column data is inserted like float values eg. £2.00, £90.00 etc...
I have tried to add the values using Sum function in sql query. It adds the value but the total amount is wrong.
I guess, as others in comments have guessed, your totamt column has a VARCHAR() data type, not the FLOAT data type you mentioned in your question.
To solve this problem the best way, you should rework your database so your totamt column has a DECIMAL(12,2) data type. Why? Strings are hard to use for arithmetic operations as you have discovered. Also, floating-point arithmetic has some quirks making it not so good for money values.
In the meantime, you need to add code to your query to edit your totamt strings so they look like numbers before you give them to the SUM() operation.
This may work for you to convert your values. It removes the currency symbols and expresses the amount as a decimal value.
CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2)) totamt
So you can then do
SELECT SUM(CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2))) grand_total,
id
FROM table
GROUP BY id
I've tried to generate custom order number. My MySQL table is like:
tblOrder
id int(11) AI; ordno varchar(50);
I try to use following function inside php block as before record added event to accomplish this.
{
$sql="select max(substr(ordno,9)) as mx from ord where substr(ordno,7,2)=month(now()) order by mx";
$rs=CustomQuery($sql);
$data=db_fetch_array($rs);
$str="OR-";
$str2=date("Ymd");
$str3=($data["mx"]+1);
$values["ordno"]="$str$str2".str_pad($str3, 5, 0, STR_PAD_LEFT);}
Problem is when I run this code it always give me same order number like OR-2014013000001 for all record entry. It does not change. Could you please help me out? Thanks in advance.
I think the real issue is being hidden by the way you're parsing the composite ordno column. Remember that SUBSTRING is 1-based, not zero-based indexing.
$sql="select max(substring(ordno,12, 5)) as mx from ord where
substring(ordno, 8, 2)=month(now()) order by mx";
...seems to be the correct parsing to get the values you want.
It really would be better to store the order data in separate fields instead of encoding meaning into a single column. You may be stuck with it this way, but if you CAN edit it, why not compute the order number on the fly by grabbing a well structured date order date column and an int order sequence column?
I'm trying to find the max value contained in two separate fields of my table.
The code I'm using in my model is:
$query = $this->db->query("select max($field1) as max_id_1 from default_table1");
$row1 = $query->row_array();
$query = $this->db->query("select max($field2) as max_id_2 from default_table1");
$row2 = $query->row_array();
return max($row1['max_id_1'], $row2['max_id_2']);
I'm a complete novice where PHP and CodeIgniter is concerned - as I'm sure my code demonstrates :)
It is working insofar as it's returning values, but not the maximum values I have in the fields. For instance I know there is a 4000 value but the highest returned is 750.
I'm wondering if this is because the fields are of type VARCHAR because although they predominantly contain numbers there are some that contain characters (- or &) or the word 'to' so I couldn't use the INT type.
Because of using VARCHAR is it failing to see that 4000 is larger than 750?
If so is there a way to cast the field contents as integer before checking for the max value, and will this be affected by the non-integer values in the fields?
All offers of help and advice is gratefully received.
Tony.
This can be done using SQL using MySQL's implicit type conversion:
select max(case when (field1+0)>(field2+0) then field1+0 else field2+0 end)
from default_table1
Using +0 would convert varchar to number and also ignore any characters that follow after the number. If you still need the original content, you can write the query like this:
select case when (field1+0)>(field2+0) then field1 else field2 end
from default_table1
order by case when (field1+0)>(field2+0) then field1+0 else field2+0 end desc
limit 1
Oh, oops, I've forgot the mysql part
You should do the same thing, cast to integer:
select max(cast($field1 to unsigned)) as max_id_1 from default_table1
It depends of your data, but you may try something like that
return max((int)$row1['max_id_1'], (int)$row2['max_id_2']);
Have a look to the PHP doc on string to int conversion
What you are really asking for is a way for codeigniter to convert the strings to numbers, and the find the max values of those. I don't know of any way to do this in codeigniter.
If you really want this, you have two options:
loop through all the rows in the table, and use php to parse out the number, while looking for the maximum number
Add a number column to the db, and do the string-to-number parsing in this new column whenever the values are inserted.
The first option is incredibly inefficient, and the second is probably your best bet.
I have some PHP code which allows me to sort a column into ascending and descending order (upon click of a table row title), which is good. It works perfectly for my D.O.B colum (with date/time field type), but not for a quantity column.
For example, I have quantites of 10, 50, 100, 30 and another 100.
The order seems to be only appreciating the 1st integer, so my sorting of the column ends up in this order: 10, 100, 100, 30, 50... and 50, 30, 100, 100, 10.
This is obviously incorrect as 100 is bigger than 50, therefore both 100 values should appear at the end surely? It seems to me that 100 is only being taken into account as having the '1' value, then it appears before 10 because the system recognises it has another 0.
Is this normal to happen? Is there any way I can solve this problem? Thanks for any help. P.S. I can show code if necessary, but would like to know if this is a common issue by default.
Sounds like you're sorting by considering those values as strings, instead of as numbers. What's doing the sorting? PHP, or MySQL? Hard to say what's wrong without seeing the code that's actually sorting it.
Your field is probably defined as char/varchar/text/..., i.e. as string type and not a numeric type. In that case lexicographical sorting performed.
The sort is treating your data as strings instead of integers. Check the datatype of your column in the database. If that is a number and not a string, then the data is being converted to strings somewhere between the database and the sort routine.
looks like you are sorting strings and not numbers, what is the data type of the column? The only way to sort string numbers properly is to add leading zeros, but it is best to store that data in the proper type, so you don't have to do these sort of manipulations each time you select the data.
In the event that you need to leave the column as a character type for some reason, you can cast to a suitable numeric type for the ordering. This is less efficient than just changing the column though.
ORDER BY CAST(theColumn AS DECIMAL(5, 2))
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
Your column is a text type. You can change the column type permanently, or cast it when you do the sort.
Table alter (you may want a different default value)
ALTER TABLE [Table]
MODIFY COLUMN [Column] INTEGER UNSIGNED NOT NULL DEFAULT '0';
Or cast when you sort
SELECT *
FROM [Table]
ORDER BY CAST([Column] as UNSIGNED INTEGER) ASC;