I have rows like this for users table:
user_id | username | user_game_count | user_password | ...
2 | testUser | 19587 | testPW | ...
I'm increasing user_game_count with UPDATE method:
UPDATE users SET user_game_count = (#cur_value := user_game_count) + 1 WHERE user_id ..
Are there any different way? Because when I use this method in a real-time game, very fast, it's giving results like this:
1
2
3
3 (skipping)
3 (skipping too)
3
3
8
...
(I'm very new to MySQL)
Related
I have a table called "MYTABLE" that looks like this:
ID | TIMESTAMP | DATA
1 | 150233939 | xxx
2 | 150233940 | xxx
3 | 150233941 | xxx
4 | 150233942 | xxx
5 | 150233943 | xxx
6 | 150233944 | xxx
7 | 150233945 | xxx
8 | 150233946 | xxx
9 | 150233947 | xxx
I can easily get all of the items using:
SELECT * FROM MYTABLE
But this table is going to get massive and it contains data points that will be shown on a graph. So instead of getting ALL of the data points, I want them spaced out. For example, I want a query that would return
1,3,5,7,9
Or:
1,4,8,9
Is there a way to do this on the query level?
UPDATE
I was able to get the data like so:
SELECT * FROM MYTABLE where mod(id,5) = 0
This divides the data quite nicely, but it is SUPER SLOW. Is there another way to make it faster?
I am using pyspark-sql to create rows in a remote mysql db, using JDBC.
I have two tables, parent_table(id, value) and child_table(id, value, parent_id), so each row of parent_id may have as many rows in child_id associated to it as needed.
Now I want to create some new data and insert it into the database. I'm using the code guidelines here for the write opperation, but I would like to be able to do something like:
parentDf = sc.parallelize([5, 6, 7]).toDF(('value',))
parentWithIdDf = parentDf.write.mode('append') \
.format("jdbc") \
.option("url", "jdbc:mysql://" + host_name + "/"
+ db_name).option("dbtable", table_name) \
.option("user", user_name).option("password", password_str) \
.save()
# The assignment at the previous line is wrong, as pyspark.sql.DataFrameWriter#save doesn't return anything.
I would like a way for the last line of code above to return a DataFrame with the new row ids for each row so I can do
childDf = parentWithIdDf.flatMap(lambda x: [[8, x[0]], [9, x[0]]])
childDf.write.mode('append')...
meaning that at the end I would have in my remote databasde
parent_table
____________
| id | value |
____________
| 1 | 5 |
| 2 | 6 |
| 3 | 7 |
____________
child_table
________________________
| id | value | parent_id |
________________________
| 1 | 8 | 1 |
| 2 | 9 | 1 |
| 3 | 8 | 2 |
| 4 | 9 | 2 |
| 5 | 8 | 3 |
| 6 | 9 | 3 |
________________________
As I've written in the first code snippet above, pyspark.sql.DataFrameWriter#save doesn't return anything, looking at its documentation, so how can I achieve this?
Am I doing something completely wrong? It looks like there is no way to get data back from a Spark's action (which save is) while I would like to use this action as a transformation, shich leads me to think I may be thinking of all this in the wrong way.
A simple answer is to to use the timestamp + auto increment number to create a unique ID. This only works if there is only one server is running at an instance of time.
:)
Hello I have maybe easy problem maybe not...
History: I wanted to replace yaml file with statuses to db(mysql) and i had in user table column: status. When i replacing logic to db i have created model Status, created table and configured relationship with user...
Describe problem: When i created status_id in user table i have 2 columns: "status" and "status_id". The column "status" is string and have a lot of string value for example "confirmed". How to (using seed migration with statuses) and fill the "status_id" column. I mean if "status" column have value "confirmed" i'd like to have in "status_id" column value: 1.
statuses table:
id name
1 confirmed
2 not confirmed
3 something else
Users table
id status status_id
1 confirmed empty
2 confirmed empty
3 confirmed empty
4 not confirmed empty
5 not confirmed empty
6 something else empty
7 something else empty
User belongs_to :status
Status has_many :users
question: Why i didnt just change name and type in "status" column on
"status_id" with data type: "id"?
answer: Because i need to deploy it using capistrano to production
server and i cant losing data and remove data from status column.
Given the following (your) sample data:
create table statuses (id int, name char(20));
insert into statuses (id, name) values
(1, 'confirmed'),
(2, 'not confirmed'),
(3, 'something else');
create table users (id int, status char(20), status_id int);
insert into users (id, status) values
(1,'confirmed'),
(2,'confirmed'),
(3,'confirmed'),
(4,'not confirmed'),
(5,'not confirmed'),
(6,'something else'),
(7,'something else');
select * from users;
+------+----------------+-----------+
| id | status | status_id |
+------+----------------+-----------+
| 1 | confirmed | NULL |
| 2 | confirmed | NULL |
| 3 | confirmed | NULL |
| 4 | not confirmed | NULL |
| 5 | not confirmed | NULL |
| 6 | something else | NULL |
| 7 | something else | NULL |
+------+----------------+-----------+
7 rows in set (0.00 sec)
This update statement updates the status_id column in users with the appropriate values from statuses:
update users u
set u.status_id=(select s.id from statuses s where u.status=s.name);
Query OK, 7 rows affected (0.01 sec)
Rows matched: 7 Changed: 7 Warnings: 0
select * from users;
+------+----------------+-----------+
| id | status | status_id |
+------+----------------+-----------+
| 1 | confirmed | 1 |
| 2 | confirmed | 1 |
| 3 | confirmed | 1 |
| 4 | not confirmed | 2 |
| 5 | not confirmed | 2 |
| 6 | something else | 3 |
| 7 | something else | 3 |
+------+----------------+-----------+
7 rows in set (0.00 sec)
Hope this is what you asked for, because my answer does neither involve ruby, yaml, nor rails.
This sounds like a one-off data migration which can be handled in several different ways. One way is to write a one-off script or rake task which will update all the users.
some_task.rake
desc 'Add status id to existing users'
namespace :users do
task assign_status: :environment do
statuses = {}
Status.all.each {|status| statuses[status.name] = status.id}
User.all.each do |user|
user.update_attribute(status_id: statuses[user.status])
end
end
end
If you have a lot of users, you may want to use find_each which is more performant as it uses batching. You may also want to add some kind of progress reporting in the rake task.
This approach allows you to test the task against a copy of your existing database.
I have a table that looks something like this:
| id | fk1 | fk2 | version |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 |
| 3 | 1 | 1 | 3 |
Having on hand the values of fk1 and fk2 I am trying to get the record with the highest value for version. Currently what I am doing is this:
version = Project.where("fk1= ? AND fk2= ?", params.require(:fk1), params.require(:fk2)).maximum(:version)
#project = Project.find_by_fk1_and_fk2_and_version(params.require(:fk1), params.require(:fk2), version)
This gets me the correct record, but I have to execute 2 queries for something that seems really simple in theory, but after trying a number of different things I had no luck with doing this with a single query. I am envisioning something like:
version = Project.where("fk1= ? AND fk2= ? AND max(version)", params.require(:fk1), params.require(:fk2))
or something.
Well the rails way to do that is
Project.where(fk1: params.require(:fk1), fk2: params.require(:fk2)).
order('version desc').first
Which translates to an sql query like:
SELECT * FROM projects WHERE fk1 = "fk1" AND fk2 = "fk2" ORDER BY version DESC LIMIT 1;
I have table like:
user :
uid | course_id | subjects
---------------------------
1 | 1 | html,php
2 | 1 | java,html,sql
3 | 1 | java
4 | 1 | fashion,html,php,sql,java
I want to run a query which can return most liked subjects in query and then second most and so on...
For Example :
select * from user where subjects like '%java%' or '%php%' or '%html%';
this query will return data like this:
uid | course_id | subjects
---------------------------
2 | 1 | java,html,sql
3 | 1 | java
4 | 1 | fashion,html,php,sql,java
but i want output like this :
uid | course_id | subjects
---------------------------
4 | 1 | fashion,html,php,sql,java
2 | 1 | java,html,sql
1 | 1 | html,php
3 | 1 | java
so the most matched subjects 1st then 2nd most matched subjects and so on....
Is there any modification in my query so that i can get this type of sorted output.
Never, never, never store multiple values in one column!
Like you see now this will only give you headaches. Normalize your user table. Then you can select normally.
It should look like this
uid | course_id | subjects
---------------------------
1 | 1 | html
1 | 1 | php
2 | 1 | java
2 | 1 | html
2 | 1 | sql
3 | 1 | java
...
or better introduce an new table subjects and then make a mapping table called course_subjects
subject
id | name
------------
1 | html
2 | sql
3 | java
...
course_subjects
uid | course_id | subject_id
---------------------------
1 | 1 | 1
1 | 1 | 2
...
Based on the way you want your results, it looks like you want to order by the number of subjects (or tags) within subject. This can be accomplished by counting the number of , (commas).
The way to count the number of occurances of a character is to subtract the original length by the length when the character is removed.
Example:
SELECT *
FROM USER
WHERE subjects LIKE '%java%'
OR '%php%'
OR '%html%'
ORDER BY ( Length(subjects) - Length(Replace(subjects, ',', '')) ) DESC;
SQLFiddle: http://sqlfiddle.com/#!2/cc793/4
Result:
UID COURSE_ID SUBJECTS
4 1 fashion,html,php,sql,java
2 1 java,html,sql
3 1 java
Note:
As juergen says it is a bad idea to store multiple values in one column.
With MyISAM storage engine you can do match against.
The simplest example:
SELECT *,
MATCH (subjects) AGAINST ('java php html') AS relevance
FROM `user`
WHERE MATCH (subjects) AGAINST ('java php html')
ORDER BY relevance DESC
In MySQL 5.6 full-text search is available with InnoDB too but needs a bit extra to make it work. For more info checkout the following post: http://www.mysqlperformanceblog.com/2013/03/04/innodb-full-text-search-in-mysql-5-6-part-2-the-queries/