MySQL date comparison to fetch dates between a given range?

AmitDiwan
Updated on 03-Oct-2019 06:20:15

134 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-08-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-09-01'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-05-10'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-06-12'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------+ | AdmissionDate | +---------------+ ... Read More

Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values

AmitDiwan
Updated on 03-Oct-2019 06:18:03

126 Views

To set a custom value for NULL values, use the UPDATE command along with IS NULL property in a stored procedure. Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(50) ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(101, NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(102, 'Mike'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(103, NULL); Query ... Read More

Autoincrement in MySQL begins from 1? How can we begin it from another number?

AmitDiwan
Updated on 03-Oct-2019 06:12:21

253 Views

The autoincrement in MySQL gives a unique number every time. By default, it starts at 1. If you want to start from another number, then you need to change the auto-increment value with the help of ALTER command or you can give value at the time of table creation.Let us first create a table −mysql> create table DemoTable (    UniqueNumber int NOT NULL AUTO_INCREMENT,    PRIMARY KEY(UniqueNumber) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable ... Read More

How to use three conditions in a single MySQL query with id, name and age of students to fetch record of a student?

AmitDiwan
Updated on 03-Oct-2019 06:07:31

155 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(50),    StudentAge int ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentAge) values('Chris', 21); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName, StudentAge) values('David', 23); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName, StudentAge) values('Bob', 22); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentName, StudentAge) values('Carol', 21); Query OK, 1 row affected (0.30 sec)Display all records from ... Read More

Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL

AmitDiwan
Updated on 03-Oct-2019 06:04:24

464 Views

For this, you can use COUNT() along with DISTINCT. The COUNT() method is to count the records. However, the DISTINCT returns distinct records, whereas COUNT() method counts those unique records. Let us first create a table −mysql> create table DemoTable (    PhoneNumber bigint ); Query OK, 0 rows affected (1.29 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(8567789898); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable values(8567789898); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values(9876564534); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable ... Read More

Only display row with highest ID in MySQL

AmitDiwan
Updated on 03-Oct-2019 06:02:55

77 Views

To order, use the ORDER BY DESC clause. With that, since we want a single ID, which should be the highest, use LIMIT 1. This will fetch the row with highest ID. Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(50) ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(110, 'Robert'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(120, 'Mike'); Query OK, 1 ... Read More

How can I get a file's permission mask using Python?

Rajendra Dharmkar
Updated on 03-Oct-2019 05:57:13

126 Views

To get stat of a file, method stat() from the os module can be used. It performs a stat system call on the given path. For example,import os st = os.stat("file.dat")This function takes the name of a file, and returns a 10-member tuple with the following contents:(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)The mode variable gives you the information about file permissions. You can get it by st[0]. You can read more about interpreting the tuple here: http://effbot.org/zone/python-fileinfo.htm

How to change the permission of a directory using Python?

Rajendra Dharmkar
Updated on 03-Oct-2019 05:53:16

4K+ Views

On a platform with the chmod command available, you could call the chmod command like this:>>> import subprocess >>> subprocess.call(['chmod', '-R', '+w', 'my_folder'])If you want to use the os module, you'll have to recursively write it:Using os: import os def change_permissions_recursive(path, mode):     for root, dirs, files in os.walk(path, topdown=False):         for dir in [os.path.join(root, d) for d in dirs]:             os.chmod(dir, mode)     for file in [os.path.join(root, f) for f in files]:             os.chmod(file, mode) change_permissions_recursive('my_folder', 0o777) This will change the permissions of my_folder, all ... Read More

How to change file permissions in Python?

Rajendra Dharmkar
Updated on 03-Oct-2019 05:51:54

2K+ Views

To change the permission of a file, you can use the os.chmod(file, mode) call. Note that the mode should be specified in octal representation and therefore must begin with a 0o. For example, to make a file readonly, you can set the permission to 0o777, you can use:>>> import os >>> os.chmod('my_file', 0o777)You can also use flags from the stat module. You can read more about these flags here: http://docs.python.org/2/library/stat.htmlAnother way to acheive it is using a subprocess call:>>> import subprocess >>> subprocess.call(['chmod', '0444', 'my_file'])

How to change the mode of a file using Python?

Rajendra Dharmkar
Updated on 03-Oct-2019 05:48:30

506 Views

To change the permission of a file, you can use the os.chmod(file, mode) call. Note that the mode should be specified in octal representation and therefore must begin with a 0o. For example, to make a file readonly, you can set the permission to 0o777, you can use:>>> import os >>> os.chmod('my_file', 0o777)You can also use flags from the stat module. You can read more about these flags here: http://docs.python.org/2/library/stat.htmlAnother way to acheive it is using a subprocess call:>>> import subprocess >>> subprocess.call(['chmod', '0444', 'my_file'])

Advertisements