Tag Archives: MySQL Queries

How to get the row number or rank of row in mysql?

It should be done as follows:

SELECT @x:= @x + 1 AS rank, title FROM t1 JOIN ( SELECT @x:= 0 )X ORDER BY weight

How to order contents using specific values in a field using MySql?

It can be done as follows:-

SELECT * FROM table_name ORDER BY FIELD(filed_name,"value_y","value_x") DESC

UPDATE multiple tables in single query?

It’s as follows :-

UPDATE table1 a INNER JOIN table2 b ON a.task_id = b.task_id SET a.column='val' b.column=UNIX_TIMESTAMP(CONVERT_TZ(CURRENT_TIMESTAMP,'+05:30','+00:00')) WHERE a.task_id='1024' AND b.task_id='1024'

INSERT a data from a table to another in a single query…

It’s as follows…

INSERT INTO table2 SELECT * FROM table1;

or to copy a few columns

INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

They can also be written as follows

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

or to copy a few columns

SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

How to get values(via some conditions) from the MySql table without using ‘OR’?

It can be done as follows :

SELECT * FROM `table_name` WHERE `request_id` IN ('value1','value2','value3','value4');

The benefit of using IN() is that it can process data more faster than using OR condition.