How do I select four random tables from a MySQL database having thousands of tables?

AmitDiwan
Updated on 01-Oct-2019 07:17:43

86 Views

To select four random tables, use ORDER BY RAND(). Following is the syntax −select TABLE_NAME AS anyAliasName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ‘yourDatabaseName’; order by rand() limit yourLimitNumber;Let us implement the above syntax in order to select four random tables from a MySQL database that has thousands of tables.Here, LIMIT is used to set the number of records you want to fetch. Since we want 4 records, therefore we would be using LIMIT 4. Following is the query −mysql> select TABLE_NAME AS Random4TableName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'web' order by ... Read More

Get the maximum value of a column with MySQL Aggregate function

AmitDiwan
Updated on 01-Oct-2019 07:16:29

111 Views

To get the maximum value of a column, MySQL has a predefined aggregate function MAX(). Let us first create a table −mysql> create table DemoTable (    Id int ); Query OK, 0 rows affected (0.96 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(988); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(99); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(999); Query OK, ... Read More

MySQL query to select all the records only from a specific column of a table with multiple columns

AmitDiwan
Updated on 01-Oct-2019 07:14:45

406 Views

To fetch records from a specific column, use the following syntax. Just select that specific column for which you want the records −select yourColumnName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score int ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Score) values(89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Score) values(99); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Score) values(78); Query OK, 1 row affected (0.11 sec) mysql> ... Read More

Is there a way to create a MySQL “alias” while creating a VIEW?

AmitDiwan
Updated on 01-Oct-2019 07:12:36

171 Views

Yes, use the AS keyword to create a MySQL alias. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100) ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+ | FirstName | +-----------+ | Chris ... Read More

MySQL ORDER BY with CASE WHEN

AmitDiwan
Updated on 01-Oct-2019 07:11:21

3K+ Views

For this, you can use the ORDER BY CASE statement. Let us first create a table −mysql> create table DemoTable order by with vas Color varchar(100) ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Red'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Green'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Blue'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Yellow'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select ... Read More

What are the best practices to organize Python modules?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:10:31

379 Views

Here is a sample project that shows a very good way to structure your projects: https://github.com/kennethreitz/samplemod. The project is about creating the "sample" module. The directory structure looks as follows:README.rst LICENSE setup.py requirements.txt sample/__init__.py sample/core.py sample/helpers.py docs/conf.py docs/index.rst tests/test_basic.py tests/test_advanced.pyThe README.rst file: This file is for giving a brief description of the module, how to set it up, how to use it, etc.LICENSE: Contains license text and any copyright claims.setup.py: It is Python's answer to a multi-platform installer and make file. If you’re familiar with command line installations, then make && make install translates to python setup.py build && python ... Read More

What is the most compatible way to install python modules on a Mac?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:09:57

173 Views

The most popular way to manage python packages (if you're not using your system package manager like homebrew) is to use setuptools and easy_install. It is probably already installed on your system. Use it like this:easy_install djangoeasy_install uses the Python Package Index which is an amazing resource for python developers. Have a look around to see what packages are available.A better and more reliable way to install python modules across platforms is to use pip. If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setuptools, but will need to upgrade ... Read More

Can we iteratively import python modules inside a for loop?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:08:32

2K+ Views

Yes you can iteratively import python modules inside a for loop. You need to have a list of modules you want to import as strings. You can use the inbuilt importlib.import_module(module_name) to import the modules. For example,>>> import importlib >>> modnames = ["os", "sys", "math"] >>> for lib in modnames: ...     globals()[lib] = importlib.import_module(lib)The globals() call returns a dict. We can set the lib key for each library as the object returned to us on import of a module.

How to install python modules without root access?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:07:43

629 Views

If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for modules in the given directory:>>> import os, sys >>> file_path = 'AdditionalModules/' >>> sys.path.append(os.path.dirname(file_path)) >>> # Now python also searches AdditionalModules folder for importing modules as we have set it on the PYTHONPATH.You can also use virtualenv to create an isolated local Python environment. The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you ... Read More

How to install libxml2 with python modules on Mac?

Rajendra Dharmkar
Updated on 01-Oct-2019 07:07:08

617 Views

You can install libxml2 using the following commands:$ brew install --framework python $ brew install --with-python libxml2 $ brew install --with-python libxslt $ brew link libxml2 --force $ brew link libxslt --forceIf you had already installed libxml2 but it had failed or any other error, you can uninstall it and then reinstall it using:$ brew unlink libxml2 $ brew uninstall libxml2 $ brew unlink libxslt $ brew uninstall libxslt

Advertisements