Sharon Christine has Published 451 Answers

Select a fixed number of random records from a MySQL table?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:35:14

For random records, you can use rand() method. To set the number of records, use the LIMIT −select *from yourTableName order by rand() limit numberOfRecords;Let us first create a table −mysql> create table DemoTable    -> (    -> LastName varchar(100)    -> ); Query OK, 0 rows affected (0.51 ... Read More

Store a variable with the result of a MySQL SELECT CASE?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:34:14

For this, use SELECT CASE. Let us first create a table −mysql> create table DemoTable    -> (    -> Num1 int,    -> Num2 int    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 30); ... Read More

Create a table named “select” in SQL databases?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:32:59

Since “select” is a reserved word in MySQL, we cannot create a table name with it. But, if you still want to create such a table, surround the word select with quote.Let us first create a table −mysql> create table `select` -> ( -> Number int -> ); Query OK, ... Read More

How to order an alphanumeric column in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:16:32

To order an alphanumeric column with values like “100X, “2Z”, etc. use the ORDER BY. Let us first create a table −mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

How to place number 0 from a column at the end maintaining the ascending search order in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:13:44

Let us first create a table −mysql> create table DemoTable    -> (    -> Number int    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.13 sec) mysql> insert ... Read More

Fix Error with TYPE=HEAP for temporary tables in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 12:11:41

The TYPE=HEAP deprecated in newer MySQL versions. You can use ENGINE=HEAP instead of TYPE. Following is the syntax −ENGINE=HEAP;Let us first create a table. Here, we have set Engine=HEAP −mysql> create TEMPORARY table DemoTable    -> (    -> StudentId int,    -> StudentName varchar(30)    -> )Engine = HEAP; ... Read More

MySQL query to order by two fields and NULL values in chronological order?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 11:51:42

Let us first create a table −mysql> create table DemoTable -> ( -> FirstName varchar(100), -> LastName varchar(100) -> ); Query OK, 0 rows affected (1.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Sam', 'Brown'); Query OK, 1 row affected (0.25 sec) mysql> ... Read More

MySQL DATE_ADD() to increment a date based on the value in another column?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 11:47:25

Let us first create a table with one of the columns as DueDate and another one “RepeatTime, which displays how many times, let’s say a user was reminded to submit the payment −mysql> create table DemoTable    -> (    -> DueDate date,    -> RepeatTime int    -> ); ... Read More

Change a MySQL column to have NOT NULL constraint

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 11:45:00

To update the constraint, use the MODIFY command. Following is the syntax −alter table yourTableName modify yourExistingColumnName yourExistingDataType NOT NULL;  Let us first create a table −mysql> create table DemoTable    -> (    -> UserId int NOT NULL AUTO_INCREMENT,    -> UserFirstName varchar(100),    -> UserLastName varchar(100),    -> ... Read More

Is there a default ORDER BY value in MySQL?

Sharon Christine

Sharon Christine

Updated on 30-Jun-2020 11:35:40

There is no default ORDER BY value in MySQL. You need to specify ORDER BY clause explicitly. Following is the syntax −ORDER BY ASC; OR ORDER BY DESC;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentName varchar(100)    -> ); Query OK, 0 ... Read More

Previous 1 ... 5 6 7 8 9 10 11 ... 46 Next
Advertisements