Equivalent of the query from sql server in mysql - mysql

I have query in SQL Server , but i don't know its equivalent in mysql.
sql code is:
select username as t ,*from users
this code running in sql but this code not run in mysql.
is there way?

You can do either
select *, username as t from users
or
select username as t, users.* from users
for some reason in MySQL if you specify specific columns first you have to qualify the *

you can use this code :
select *,username as t from users

Related

SQL Like with double pipe concatenation

I'm having a problem with a SQL query that must match the username of a user out of a column that contains all the users usernames.
So the column will contain something like:
|USER1|USER2|USER3|USER11|USER22|
The user have pipes on the left and right to prevent "USER1" be matched even in "USER11".
My query is
SELECT * FROM myTable WHERE CONCATUSERS LIKE ('%|' || 'USER1' || '|%')
Note that the USER1 in the query is a variable generated from our code so I must keep the concatenation syntax and I must use a standard syntax too (the code will run in mySQL, SQLServer etc..
So what is the correct way of concatenating strings in a LIKE clause?
MySQL uses the double pipes for concat. SQL Server you can use +.
SELECT *
FROM myTable
WHERE CONCATUSERS LIKE ('%' + '|user1|' + '%')
Use CONCAT.
(available on SQL server 2012 and beyond)
It has the benefit that it implicitly converts types to add the value to the string. And it's not just available on Sql Server and MySql.
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT('%|','USER1','|%');
Do note that in MySQL the result will be NULL if one of the concatenated values is NULL. But not on Sql Server.
It's just Oracle that's being stubborn by only allowing 2 values to that function.
So if the SQL needs to run unchanged on MySQL, a recent Sql Server AND Oracle then this should work:
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT(CONCAT('%|','USER1'),'|%');
You can go with CONCAT function.
As it is supported in both SQL and MySQL
SELECT *
FROM xpat
WHERE plname LIKE concat('%' ,'|user1|' ,'%');

mysql datetime field matching query not working correctly

I have written a mysql query
select * from user where registration_date="2016-05-20"
But it returns wrong value. Please help me
Use
select * from user where date(registration_date)=date("2016-05-20")

What is MYSQL equivalent for DATABASENAME..TABLENAME?

In MSSQL Server, I can execute the following SQL
SELECT * FROM DATABASENAME..TABLENAME
or
SELECT * FROM DATABASENAME.dbo.TABLENAME
What is the equivalent syntax on MYSQL to perform this SQL (DATABASE..TABLE)?
If you want to select all the records from a database table the syntax is quite similar it's
SELECT * FROM DatabaseName.TableName
You can do below
SELECT * from mydatabase1.tblName

Can not Access Aliased Columns in Select Statement (MySQL)

I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.

MySql using correct syntax for the over clause

What is the correct syntax to get the over clause to work in mysql?
I would like to see the total sms's sent by each user without grouping it with the group by clause.
SELECT
username,
count(sentSmsId) OVER (userId)
FROM
sentSmsTable,
userTable
WHERE
userId = sentUserId;
MySQL 8 has got the window functions! Therefore, you can write your query in it like this:
SELECT username,
count(sentSmsId) OVER (partition by userId)
FROM sentSmsTable
JOIN userTable ON userId = sentUserId;
There is no OVER clause in MySQL that I know of, but here is a link that might assist you to accomplish the same results:
http://explainextended.com/2009/03/10/analytic-functions-first_value-last_value-lead-lag/
Hope this helps.
MySQL does not currently support window functions, so over() will only yield syntax errors (or garbage, if it's accepted regardless).
MySQL Doesn't have window functions until the most recent release: MySQL 8 (release in April, 2018). MS SQL Server also accepts OVER clause.
The syntax is:
function(col1) OVER (PARTITION BY col2 ORDER BY col3)
Check out https://mysqlserverteam.com/mysql-8-0-2-introducing-window-functions/ for more examples.