MySQL query to SQL-Server - mysql

I have a mysql query that I have to convert to sql server syntax, I am novice and perhaps someone can help me. Here is my code:
SELECT id, nick, mobile, name, description, direction, date, image FROM mytable WHERE number=1 ORDER BY date desc LIMIT 1, 10;
Is there some tool to try sql server querys or some online converter
Thanks for all

For SQL 2012, you have the rather unwieldy syntax:
SELECT id, nick, mobile, name, description, direction, date, image
FROM mytable WHERE number=1
ORDER BY date desc
OFFSET 100 ROWS FETCH NEXT 5 ROWS ONLY;
For SQL 2005, you generally need to adopt a 2-step approach to pagination via a derived table, e.g.
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY date desc) As RowID,
id, nick, mobile, name, description, direction, date, image
FROM mytable
WHERE number=1
) As RowResults
WHERE RowID BETWEEN 101 AND 106;
SqlFiddle here

UPDATED :
select * FROM
(
select ROW_NUMBER() OVER (ORDER BY ID) as RowNumber , * FROM mytable
) tmp
where tmp.RowNumber between 15 AND 30

Related

How to ORDER BY with UNION in SQL with column ALIASES?

Here is my very simple MYSQL request :
(SELECT start, name, id, info FROM `table1`)
UNION
(SELECT end, name, id, info FROM `table1`)
I want to sort the result by 1st column and I guessed I need to use aliases:
(SELECT start as mydate, name, id, info FROM `table1`)
UNION
(SELECT end as mydate, name, id, info FROM `table1`)
ORDER BY mydate
I was surprised that MYSQL threw this error :
"Unknown column 'mydate' in 'where clause'"
I ended with this working bad practice :
(SELECT start, name, id, info FROM table1)
UNION
(SELECT end, name, id, info FROM table1)
ORDER BY 1
source: https://www.mysqltutorial.org/sql-union-mysql.aspx
But I would like to understand my error !
Use a nested query.
SELECT q.*
FROM (
SELECT start as mydate, name, id, info FROM `table1`
UNION ALL
SELECT end as mydate, name, id, info FROM `table1`
) AS q
ORDER BY q.mydate
The inner query builds up your result set and the outer one orders it. The MySQL query planner is reasonably smart about optimizing this sort of thing.
By the way UNION removes duplicates and UNION ALL does not.

order by with union in SQL is not working

Is it possible to order when the data comes from many select and union it together? Such as
In this statement, the vouchers data is not showing in the same sequence as I saved on the database, I also tried it with "ORDER BY v_payments.payment_id ASC" but won't be worked
( SELECT order_id as id, order_date as date, ... , time FROM orders WHERE client_code = '$searchId' AND order_status = 1 AND order_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
UNION
( SELECT vouchers.voucher_id as id, vouchers.payment_date as date, v_payments.account_name as name, ac_balance as oldBalance, v_payments.debit as debitAmount, v_payments.description as descriptions,
vouchers.v_no as v_no, vouchers.v_type as v_type, v_payments.credit as creditAmount, time, zero as tax, zero as freightAmount FROM vouchers INNER JOIN v_payments
ON vouchers.voucher_id = v_payments.voucher_id WHERE v_payments.client_code = '$searchId' AND voucher_status = 1 AND vouchers.payment_date BETWEEN '$start_date' AND '$end_date' ORDER BY v_payments.payment_id ASC , time )
UNION
( SELECT return_id as id, return_date as date, ... , time FROM w_return WHERE client_code = '$searchId' AND w_return_status = 1 AND return_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
Wrap the sub-select queries in the union within a SELECT
SELECT id, name
FROM
(
SELECT id, name FROM fruits
UNION
SELECT id, name FROM vegetables
)
foods
ORDER BY name
If you want the order to only apply to one of the sub-selects, use parentheses as you are doing.
Note that depending on your DB, the syntax may differ here. And if that's the case, you may get better help by specifying what DB server (MySQL, SQL Server, etc.) you are using and any error messages that result.
You need to put the ORDER BY at the end of the statement i.e. you are ordering the final resultset after union-ing the 3 intermediate resultsets
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. See link below:
ORDER BY and LIMIT in Unions
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;

How to select all the rows having same and latest value in a particular column from database mysql

I want to select all the rows that are having the same and latest time in a particular column i.e., time1. The below image gives a brief idea about my question. Please guide me with this. Thank you in advance.
Here is a solution that relies on window function RANK(), available since MySQL 8.0:
SELECT *
FROM (SELECT id, name, time1, RANK() OVER(ORDER BY time1 DESC) rnk FROM mytable) x
WHERE rnk = 1
The inner query assignes a rank to each record, sorted by descending time; top ties get the same rank, 1. Then the outer query just filters in records having rank 1.
You could use a sunquery for max time
select * from my_table
where time1 = (
select max(time1)
from my_table
)
Please try this ,
select * from table1 where time1 =(select time1 from table1 order by time1 limit 0,1);
or
select * from table1 where time1 = ( select max(time1) from table1 )
or
select * from table1 where time1=(select TOP 1 time1 from table1 order by time1 desc )
DEMO

Turn MySQL query to SQL Server

I have this query in MySQL:
Select *
From Customer
Order By ID DESC Limit 1,1
How to use this query for SQL server ?
For SQL Server 2005 and up:
;WITH cte AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY ID DESC) AS RowNumber
FROM Customer
)
SELECT *
FROM cte
WHERE RowNumber = 2
MSSQL, use this query to fetch Nth record
SELECT * FROM (
SELECT
*, ROW_NUMBER() OVER(ORDER BY userID) AS ROW
FROM tblUser
) AS TMP
WHERE ROW = n
In SQL Server 2012 or higher, you can also use Offset and Fetch:
Select *
From Customer
Order By ID Desc
Offset 1 Rows
Fetch Next 1 Rows Only

Equivalent of MSSQL with statement in MYSQL

is there any equivalent to the following statement in mysql ?
With Tmp1 as (
Select Distinct EmpID, TypeID
From tb_deductionBalance
), Tmp2 as (
Select *,
row_number() OVER ( order by empID /* no employeeID in Tmp1 */) as RowNum
From Tmp1
)
Select * From Tmp2
Where RowNum Between #Start and #End
I have to migrate an mssql database to mysql and there are plenty of such statements which would take much more time to recreate in mysql if it cannot be translated.
Thanks
SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT YourStartNumber - 1, YourEndNumber - YourStartNumber
Unfortunately, you can't use variables in LIMIT, so you'll need to use actual integers there. For instance, if you want results from the rows 3 to 10, you'll need to use:
SELECT DISTINCT EmpID, TypeID
FROM tb_deductionBalance
ORDER BY empID
LIMIT 2, 7