I realize we can use substring() and locate() function for tokonization
I was tried query
insert into sum_of_counts
select substring(pair,1,locate('|',pair)),
sum(count)
from em
group by substring(pair,1,locate('|',pair))
for example : we use 'resumption|||resumption' as pair to query after substring('resumption|||resumption',1,locate('|','resumption|||resumption'))
it should be resumption|
but after the query it appeared
+------------+------+
| wild_pair | sum |
+------------+------+
| resumption | 8 |
+------------+------+
the problem is we could find 'resumption|||resumption' in em table
after I check the table sum_of_counts some of wild_pair are word| some of wild_pair are just word how can I solve this problem?
SELECT
LEFT(`pair`, LOCATE('|||', `pair`)) `wild_pair`,
SUM(count) `sum`
FROM `em`
GROUP BY `wild_pair`;
Should do the same thing easier.
If the error occurs when inserting the result into another table, check if the existing columns are wide enough to take the calculated data in full length.
Your result should not be possible:
mysql> select locate('|', 'resumption|||resumption');
+----------------------------------------+
| locate('|', 'resumption|||resumption') |
+----------------------------------------+
| 11 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select substring('resumption|||resumption', 1, 11);
+---------------------------------------------+
| substring('resumption|||resumption', 1, 11) |
+---------------------------------------------+
| resumption| |
+---------------------------------------------+
1 row in set (0.00 sec)
Are you sure that pair is always foo|||bar with 3 |||?
Related
I have a Table with a column named Class which contains school class names like 10A, 5B, 7C etc. I'm looking for a method to get this column divided in two parts: The number and the character, so that the output is for example:
class: 10;
classID: A
I've read about substring_index, but I think I can't use it for my problem.
Thanks for your help!!
a very very tricky method
as a recommendation, you had better redesign your database schema :D
mysql> select * from f;
+------+
| c |
+------+
| 10AB |
+------+
1 row in set (0.00 sec)
mysql> select substr(c from 1 for length(c*1)) as Class, substr(c from length(c*1)+1) as ClassId from f;
+-------+---------+
| Class | ClassId |
+-------+---------+
| 10 | AB |
+-------+---------+
1 row in set (0.00 sec)
So, with mysql, why do things that don't equal each other, equal each other? For example, why ...
mysql> SELECT '3' = 3;
+---------+
| '3' = 3 |
+---------+
| 1 |
+---------+
Just, why?
And more importantly ...
mysql> SELECT 0 = '';
+--------+
| 0 = '' |
+--------+
| 1 |
+--------+
But, why?
Also ...
mysql> SELECT '3x' into #foo;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT #foo, CAST(#foo as signed), #foo = CAST(#foo as signed);
+------+----------------------+-----------------------------+
| #foo | CAST(#foo as signed) | #foo = CAST(#foo as signed) |
+------+----------------------+-----------------------------+
| 3x | 3 | 1 |
+------+----------------------+-----------------------------+
Dear God, why?
But worse still ...
mysql> SELECT '3x', CAST('3z' as signed), '3x' = CAST('3z' as signed);
+----+----------------------+-----------------------------+
| 3x | CAST('3z' as signed) | '3x' = CAST('3z' as signed) |
+----+----------------------+-----------------------------+
| 3x | 3 | 1 |
+----+----------------------+-----------------------------+
Why, oh why? Why does it make me cry so ... ?
It's all there in the documentation:
http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html
Especially in the line
In all other cases, the arguments are compared as floating-point (real) numbers.
So all your comparisons are floating-point comparisons and as such make perfect sense.
One can argue wether automatic type-conversion makes sense at all (should it be possible to compare '1' to 1?)...
I have a table which stores times as a simple varchar, in a format that looks like "2012-03-27T03:03:00".
I'd like to use mysql's date functions with this data. Some functions work fine, i.e.
mysql> select year('2012-03-14T11:28:32'), month('2012-03-14T11:28:32');
+-----------------------------+------------------------------+
| year('2012-03-14T11:28:32') | month('2012-03-14T11:28:32') |
+-----------------------------+------------------------------+
| 2012 | 3 |
+-----------------------------+------------------------------+
1 row in set (0.00 sec)
But hour and minute functions fail:
mysql> select hour('2012-03-14T11:28:32'), minute('2012-03-14T11:28:32');
+-----------------------------+-------------------------------+
| hour('2012-03-14T11:28:32') | minute('2012-03-14T11:28:32') |
+-----------------------------+-------------------------------+
| 0 | 20 |
+-----------------------------+-------------------------------+
1 row in set, 2 warnings (0.00 sec)
I believe it is the 'T' in my date format that screws up mysql. If I manually replace it with a space, the functions work:
mysql> select hour('2012-03-14 11:28:32'), minute('2012-03-14 11:28:32');
+-----------------------------+-------------------------------+
| hour('2012-03-14 11:28:32') | minute('2012-03-14 11:28:32') |
+-----------------------------+-------------------------------+
| 11 | 28 |
+-----------------------------+-------------------------------+
1 row in set (0.00 sec)
Is there some simple way I can tell mysql to parse the 'T'???
Those MySQL Date and Time functions (HOUR, for example) require a specific part of the timestamp.
Therefore, you need to use TIME and DATE to extract the time and date, respectively, from the passed timestamp:
Extracting the time:
select hour(time('2012-03-14T11:28:32')), minute(time('2012-03-14T11:28:32'));
Extracting the date:
select year(date('2012-03-14T11:28:32')), month(date('2012-03-14T11:28:32'));
SELECT HOUR(STR_TO_DATE('2012-03-14T11:28:32','%Y-%m-%dT%k:%i:%s'))
STR_TO_DATE is pretty good about returning NULL for rubbish dates like this non-leap-year item.
SELECT STR_TO_DATE('2011-02-29T11:28:32','%Y-%m-%dT%k:%i:%s')
It also lets you do stuff like
SELECT STR_TO_DATE('2011-02-28T11:28:32','%Y-%m-%dT%k:%i:%s') + INTERVAL 1 QUARTER
or
SELECT STR_TO_DATE('2011-02-28T11:28:32','%Y-%m-%dT%k:%i:%s') - INTERVAL 35 MINUTE
and have everything work right.
See here for the conversion specifiers.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
I have a query that retrieves some data, among those data I have some that are returned with a value like 0. I would like the query to NOT return the columns when that's the case.
How can we do such a thing?
Regards,
MEM
select <column_name> from <table_name> where <column_name> <> 0.0
Here is all the data in a sample database. Notice how there are 3 rows with one having a zero value for the num column.
mysql> select * from test_tbl;
+------+----------+
| num | some_str |
+------+----------+
| 0 | matt |
| 2 | todd |
| 3 | Paul |
+------+----------+
3 rows in set (0.00 sec)
Now lets use the where clause to specify the rows we want to ignore (it's a little bit of reverse logic because we are actually specifying what rows we want).
mysql> select * from test_tbl where num <> 0.0;
+------+----------+
| num | some_str |
+------+----------+
| 2 | todd |
| 3 | Paul |
+------+----------+
2 rows in set (0.00 sec)
Note: This will only work without getting messy if 0 is the only value you are worried about. A better way would be to allow nulls in your column and then you can check to see if they are non-null in the where clause.
SELECT 1 FROM (SELECT 1 FROM table) q
does not working on my local server. it returns totally nothing (no error or empty table).
But SELECT 1 FROM table is ok.
What is wrong with it? is there any MySQL option for such queries?
Without really being able to understand what you're wanting to achieve, the solution to this problem is:
SELECT 1 FROM (SELECT 1) q;
Assuming that you have a table called "table", the SQL's fine. In my own database:
mysql> select 1 from (select 1 from ui_towngrpcolour) foo;
+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
+---+
3 rows in set (0.00 sec)