How to use auto increment to save log - mysql

I want to make a table to save the user log ( for example : login record)
Can the table check the id when using the auto increment ?
Example :
userid times login_record
1 1 xxxxx
2 1 xxxyy
1 2 xxyyy
1 3 xyyyy
2 2 xxxxx

not the way you mean, no. Mysql will not automatically look up the last row in the logins table with a matching user_id and increment the times field.
the mysql auto-increment is for the internally generated row id, not for column values.
If you insert one row for every login, you can get the user logins count by querying the table, select count(*) from logins where user_id = 1. If you add an index on user_id, this query will run pretty quick.

Related

how to select specific rows in some condition

I tried to write a query that selects rows with steps that both user 1 and user 2 did, with combined number of times they did the step (i.e., if user 1 did step 1 3 times and user 2 did 1 time then the count should show 4 times.)
when I put condition as user_id=1, user_id=2 there is no error but it return nothing, when it should return some rows with values.
there is table step, and step taken
and table step has column id, title
table step_taken has column id, user_id(who performs steps), step_id
i want to find step that both of two user whose id 1,2 did
and also want to have the value as count added up how many times they performed that step.
for example if user id 1 did step named meditation 2 times,
and user id 2 did step named meditation 3 times,
the result i want to find should be like below ;
------------------------------
title | number_of_times
------------------------------
meditation| 5
------------------------------
here is my sql query
select title, count(step_taken.step_id)as number_of_times
from step join step_taken
on step.id = step_taken.step_id
where user_id = 1 and user_id=2
group by title;
it returns nothing, but it should return some rows of step both user1 and user 2 did.
when i wrote same thing only with user_id=1 or user_id=2, it shows selected information
how can I fix my code so it can show the information I want to get?
thanks in advance :)
user_id cannot be 1 and 2 at the same time. You need a second user table. Then join those on your criteria and count:
select title, count(u1.id) + count(u2.id) as number_of_times
from step u1 join step u2
on u1.id = u2.id
where u1.user_id = 1 and u2.user_id=2
group by title;
note: cannot tell what table title is in, or the purpose of step_taken was as step.id is identical.

How to get history of mapping for following table data

I have a history mapping table for UserId changes, where every time when UserId changes, a row for new UserId with old UserId inserted in the history table.
Below is the sample table and data:
UserIdNew | UserIdOld
---------------------
5 | 1
10 | 5
15 | 10
The above data explains that UserId 1 has gone with following transition from UserId 1 -> 5-> 10 -> 15.
I want to query all the Old Ids for a give UserIdNew, how can I do it in a single query?
For this case if UserIdNew = 15, then it should return 1,5,10
If UserIdNew are always greater then previous (older) in a UserIds chain, i.e. if cases like 10->20->5->1 never happen, this query can do the job (not fully tested, new and old used instead of your field names):
SELECT
CASE
WHEN new=7 THEN #seq:=concat(new,',',old)
WHEN substring_index(#seq,',',-1)=new THEN concat(#seq,',',old)
ELSE #seq
END AS SEQUENCE
FROM (SELECT * FROM UserIdsTable ORDER BY new DESC) AS SortedIds
ORDER BY SEQUENCE DESC
LIMIT 1

getting data from two non related tables where the date of visit should be followed up after a certain period of time

I have a table “InitialVisit” which records date the user visited for a particular purpose, these visits could be duplicated but can be differentiated by the visit date and purpose, I have another table “SubsequentVisit” which has the subsequent visits after the initial visit, which has data available only one year. The “InitialVisit” Table is historic and has five year worth of data, but is not too large, but the “SubsequentVisit” is very large more than 50M records.
I want to find the subsequent visit by the user after the date left from the second table in one month. The data is collected raw so there is no primary or foreign keys involved
The data snippet is
“InitialVisit”
UserID DateVisit `DateLeft ` `Purpose`
1 `01-01-2016` 02-01-2017 F20
2 23-11-2016 12-12-2016 R43
1 03-03-2016 04-04-2016 F27
3 06-07-2014 09-07-2014 K22
4 09-09-2016 10-09-2016 Y77
5 04-07-2016 02-08-2016 F22
“SubsequentVisit”
UserID SubsequentVisit
1 03-01-2017
1 20-04-2016
2 27-12-2016
I would really appreciate a simple and fast query where I can get the result
UserID
3
4
5
Is there a quicker way to achieve this?
As the tables do not have any keys/indices, you can try adding an index on UserID column, e.g.:
ALTER TABLE InitialVisit ADD INDEX (UserID);
ALTER TABLE SubsequentVisit ADD INDEX (UserID);
To get all the users who do not have any subsequent visits, you can use EXISTS, e.g.:
SELECT UserID
FROM InitialVisit iv
WHERE
iv.DateLeft BETWEEN "2017-01-01" AND DATE_ADD("2017-01-01", INTERVAL 6 MONTHS)
AND NOT EXISTS (
SELECT 1 FROM SubsequentVisit WHERE UserID = iv.UserID
);

Select all values from one table, check another table to see related columns and fetch more values

I really dont know how to phrase my question, probably why google is not giving me results that i need, but am going to try.
I have two tables, required_files table and submitted_files table. I have a page where i want to display to a user all required files for submission and show which files he/she has submitted.
Required files table is as follows:
file_id file_name mandatory
1 Registration Certificate 0
2 KRA Clearance 1
3 3 Months Tax returns 0
4 Business Permit 1
5 Tour Permit 1
6 Country Govt Operating License 0
7 Certificate of good Conduct 0
file_id is unique, mandatory column is binary value to state whether the file is mandatory before registration or not.
submitted files table is a follows
file_id user_id file_required_id original_file_name file_name_on_server submission_date
1 2 2 KRA_Form.docx 0a10f5291e9bcb6a345ac7a8f5705b8a.docx 2016-11-01
2 2 3 Tax_returns.docx 9f04361013df7e25235a03c506f347ed.docx 2016-11-03
3 3 3 Taxes.docx 86aea74cc87fb669510d9d4c488cbcf8.docx 2016-11-04
file_id is unique AI value, user_id col is unique value of the current user logged in, file_required_id column is related to files_required.file_id column
When fetching the values i already have a user_id (in this case, lets use user_id = 2) Now i want to fetch all values of files_required table and check on files submitted table for files that user_id = 2 meaning user has submitted the files.
my sql query is as follows
SELECT files_required.*, submitted_files.* FROM submitted_files
RIGHT JOIN files_required ON files_required.id = submitted_files.file_required_id
WHERE submitted_files.user_id = 2
This gives me two rows only where the user_ids matched but i want the entire files_required table values and show which files the user has submitted. Someone Kindly assist.
In the meantime, i am fetching files_requied table first then looping through the other table using a php script to look for submitted files for the given user. it works but its not what i wanted and is cumbersome and a rookie move.
Try having user_id condition in RIGHT JOIN itself like below query
SELECT files_required.*, submitted_files.*
FROM submitted_files
RIGHT JOIN files_required ON files_required.id = submitted_files.file_required_id
AND submitted_files.user_id = 2
You want this.
SELECT submitted_files.user_id, files_required.*, submitted_files.*
FROM submitted_files
RIGHT JOIN files_required ON files_required.id =
submitted_files.file_required_id
Don't put the where condition on userid as it will filter out the data just for that user. You want all the records and user should also be seen. Just put the user_id in the select statement.

Selecting a Max Value based on another variable in SQL

I am trying to create an accurate count of student enrollment. There is an individual record for every course registration and any change to that particular course registration results in another record with an incremental sequence number. My table looks something like this:
ID Course Number Sequence Number
1 B101 1
1 B101 2
1 B101 3
1 C201 1
1 C201 2
2 E215 1
2 J320 1
2 J320 2
I would like to select the max value of sequence number such that every course registration is retained. This would mean that ID 1 would have 2 records. One would be B101 with sequence number=3 and another record for C201 with sequence number=2.
SELECT id, course_number, MAX(sequence_number) FROM table GROUP BY id, course_number;
You should first group all items on id then group them on Course_Number and show only the maximum value of Sequence_Number .
select id,Course_Number,max(Sequence_Number) from TblName group by id,Course_Number