I have a table
course chapter lessons
2 Chapter1 3,4
2 Chapter2 5,10,9,6,8
2 Chapter3 11,15,16,18
I need the last value in lessons column like
In the 1st row of lessons i need the last digit 4
In the 2nd row of lessons i need the last digit 8
In the 3rd row of lessons i need the last digit 18
Is storing a comma separated list in a database column really that bad? Short answer: Yes, it is.
But until you normalize the table, you could use this:
SELECT course
, chapter
, SUBSTRING_INDEX(lessons, ',', -1) AS last_lesson
FROM tableX
Related
hello I have an as3 app. this app divides the users into groups, every group consisting of 3 users.
in my mysql database there is a field in "users_into" table that identifies the number of user in his group.
this field is called "num_in_group" and its value must be a number between 1 and 3 for every user.
For clarification
The first user who registered in the application will have number 1 and the second one will have number 2 and the third one will have number 3 ---and the forth one will have 1 (not 4) ----- and fifth one will have 2 and sixth one will have 3 and seventh one will have 1 again and so on ......
so my question is how can I make the field have numbers 1 , 2 , 3 , 1 , 2 , 3 in the order constantly
One option which you might find acceptable would be to use ROW_NUMBER() and generate this value at the time you query:
SELECT
id,
1 + (ROW_NUMBER() OVER (ORDER BY id) - 1) % 3 AS seq
FROM yourTable
ORDER BY id;
The above assumes that id is an auto increment column, with a unique idea for each user. The only potential problem with this approach is that the id might not always be sequential or increasing. Assuming you can tolerate the vast majority of users have an even spread of 1, 2, and 3 values, then the above might be acceptable to you.
I have more than 50000 records in my table with two columns (id and basic) looks like this
ID
BASIC
1
XXX111XXX111
2
XXXX22221111
3
111XXXXX2212
4
2X1X212X1X1X
5
X21X12X1X12X
What I need is to display only records that consist of 5 "X" that are not adjacent. for example, from the above records i need to get data like this
ID
BASIC
4
2X1X212X1X1X
5
X21X12X1X12X
What query will suite to retrieve such record from my database.
I interpret this as meaning that you want 5 Xs that are not adjacent. I think this does what you want:
where concat(' ', col, ' ') regexp '([^X]+[X]){5}[^X]'
The concat() just takes care of the situation where the first or last character is an "X".
In this question it answered how to add two different datasets,
SSRS - Expression using different dataset fields
I tried that kind of approach and here is my expression
=First(Fields!count.Value, "remclass1")+First(Fields!count.Value, "db_IACS")
the problem is that only the first value is being Calculated like this
I added the Pulled out COUNT to the Expired Count, i want it to be added per Row
only the first part
For instance:
Column 7 (Count) = Column1(Count) + Column 4 (count) //**per Row**
The value of row 1 column 7 would be column 1 + column 4 row 1
the value of row 2 column 7 would be column 1 + column 4 row 2
The Pulled out table
(Consists of Count, Grams, Principal) are on the data set db_IACS
and the Expired table(Consists of Count,Grams,Prinicipal) are in the dataset remclass1
So any help how to do it? how to add the two columns (in a different dataset)
Thanks :)
You could do this in SQL, as follows:
SELECT db_IACS.count as PulledOut_Count, db_IACS.Grams AS PulledOut_Grams, db_IACS.Principle AS PulledOut_Principle,
remclass1.count as remclass1_Count, remclass1.Grams AS remclass1_Grams, remclass1.Principle AS remclass1_Principle
FROM db_AICS
LEFT OUTER JOIN remclass1 ON db_IACS.Id = remclass1.Id
Then just deal with everything in the one dataset.
Referring to my previous questions about the group concat
Mysql again, group by and display rest of rows
i need to get first and last day from that query
for example
row 3 from 8,9,10 to first collumn 8, last collumn 10
row 5 from 21,22,23,24,28,29,30 to first collumn 21, last collumn 30
row 6 from 17,21,22,23,24,25 to first collumn 17 last collumn 25
SUBSTR(GROUP_CONCAT(DAY),-1) as fl
BUT it gives me last char, and there are few rows with 1 or 2 chars for example
1,2,3,22
1,3,6,3
In first example it gaves me 2, not 22 :/
Another option (besides Michael's solution) is to use the SUBSTRING_INDEX:
SUBSTRING_INDEX(str,delim,count)
with
count = 1
you get the first day,
with
count=-1
you get the last one
Don't waste your time trying to parse the first and last off of GROUP_CONCAT(). Instead, just stick the MIN() and MAX() together with CONCAT().
SELECT
user,
CONCAT(MIN(DAY)), ',', MAX(DAY)) AS f1
FROM yourtable
GROUP BY user
I want to make a search within a varchar column that return the rows which has letters in its last three part. Like:
id name
--- -----
1 06jesq12g
2 06jesq123
3 06jesq126
4 06jesq12f
i want the records with id 1 and 4 because they have "letter" in the last three part. Not all of them are numeric. I hope i made my point, sorry for my english :)
Using SQL:
SELECT *
FROM ...
WHERE name REGEXP ('([a-z]..$|[a-z].$|[a-z]$)')