MySQL - Class name as 2 columns - mysql

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)

Related

MySql selecting context limited by number starting from another one, no duplicate

Hey I need to make a MySql query and get from it some number of user activities, lets say 10, then after scrolling on page I need to take another portion of activities stored in DB and start from 10 to 20 and so on... As I made this already by loading the whole DB Content for user and then dynamically show it with AJAX and jQuery I need to change the method I am doing this. So my query looks like this:
SELECT some rows FROM table WHERE User_ID = #memberID ORDER By date LIMIT limit
As this query works to take only limited records from DB I have no idea how to make a parameter that would determine which records should we take now. The problem starts when user refreshes the page - we want to start from 0 and again go 10 by 10 down.
EDIT: I am giving the query 2 params (LIMIT and OFFSET) and then in jQuery function gonna try to increase both of them.
you can do it like this
example
mysql> SELECT * FROM MAXWELL;
+------+-------+
| ID | NAME |
+------+-------+
| 3 | TWO |
| 4 | FOUR |
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
+------+-------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 0,2;
+------+------+
| ID | NAME |
+------+------+
| 3 | TWO |
| 4 | FOUR |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 2,4;
+------+-------+
| ID | NAME |
+------+-------+
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
| 10 | ten |
+------+-------+
4 rows in set (0.00 sec)

How to correctly string token in MySQL

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 |||?

How to query on an array?

I have an object like this in drill:
{MyFruit: [{name:Mike, age:10},{name:Jacob,age:9},{name:William, age:6}]}
I can get "Mike" by doing:
Select MyFruit[0].name
Is there a way for me to get the list of every single "name"? I tried the following and it does not like it:
Select MyFruit[*].name
Given this fruits.json file:
{"MyFruit": [{"name":"Mike", "age":10},{"name":"Jacob","age":9},{"name":"William", "age":6}]}
The Drill statement is:
select t.flatdata.name from (select flatten(MyFruit) as flatdata from dfs.`/Users/path/fruits.json`) t;
+----------+
| EXPR$0 |
+----------+
| Mike |
| Jacob |
| William |
+----------+
3 rows selected (0.14 seconds)
You need to use a subquery that flattens the complex nesting and table aliases, t and flatdata, that resolve ambiguities.

DBIx::Class: Only select results where has_many greater than zero

In our MySQL database, I have a third_party_accounts table and it has_many third_party_campaigns. However, not all accounts will have campaigns. What I would like to do in DBIx::Class is select only those accounts which have one or more campaigns. The simplest was I've found is as follows:
my $third_party_account_rs = $schema->resultset('ThirdPartyAccount');
my $with_campaigns_rs = $third_party_account_rs->search(
{ third_party_account_id => \'IS NOT NULL' },
{
join => 'third_party_campaigns',
group_by => 'me.id',
}
);
For the relevant database columns:
mysql> select id from third_party_accounts;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
mysql> select id, third_party_account_id from third_party_campaigns;
+----+------------------------+
| id | third_party_account_id |
+----+------------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
+----+------------------------+
3 rows in set (0.00 sec)
This seems like such an obvious use case that I'm sure there's a simple way to do this, but I can't find it.
my $with_campaigns_rs =
$schema->resultset('ThirdPartyCampaigns')
->search_related('third_party_account');

SQL (mysql) - If a given row on a given column as a certain value, don't list that column

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.