What is MYSQL equivalent for DATABASENAME..TABLENAME? - mysql

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

Related

What's the equivalent syntax in MySQL for `with xxx AS ()`

In MSSQL, I have a query something like:
WITH temp AS (
SELECT
*
FROM
xx.xxxx
)
But WITH temp AS () isn't supported in MySQL syntax, I wonder what the equivalent syntax should be in MySQL Workbench? Thanks.
Below is a screenshot of the syntax error:
If you have MySQL 5.x and you need to adapt the query which contains CTE, then convert
WITH cte AS (cte query text)
SELECT ...
FROM cte
...
to
SELECT ...
FROM (cte query text) cte
...
If there is more than one CTE then perform this substitution with accuracy from latter CTE to former one (you may need to use multiple copies of some CTE subquery text - this is a norma).

php prepared statement multiple possible querys

I'm using prepared statement for MySQLi PHP which is all working. However, the application that I'm using this in has 108 different possible statements all very similar that can be run either without condition:
select * from table1
or with
select * from table 1 where user_level = 1)
The question that I'm asking: Is there a way that I can create the statement that covers all possibilities such as
select * from table 1 where user_level = {special input}
that will give the same outcome as
select * from table1
Otherwise I'm looking at a lot of repetition.
You would use parameter binding. See https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php.

How do i get all table names who match a specific regex in mysql 8.0?

I'm trying to retrieve all tables with specific name format,
for performing union among those tables.
I'm using mysql Ver 8.0.13, and i wrote this following query for retrieving the relevant tables:
show tables LIKE REGEX '^table_.+_class$';
I couldn't figure out the correct syntax for this query :/
Afterwards i'm planning to union all those tables.
I would like to avoid writing this code since it doesn't scale nicely:
SELECT * FROM table_french_class
UNION
SELECT * FROM table_history_class
UNION
SELECT * FROM table_pingpong_class
UNION
SELECT * FROM table_math_class
UNION
SELECT * FROM table_literature_class
Can someone suggest me how to handle this issue?
Thank you
You could use INFORMATION_SCHEMA catalog:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME REGEXP '^table_.+_class$';

Union Query In Separate Databases

I have 2 databases. In database1, I want to run a query from database2 and UNION ALL the results. This is the syntax I tried, but I get an error of
Syntax error in from clause
Here is the syntax I tried --- where is my error?
SELECT * FROM query1
UNION ALL
SELECT * FROM query1 IN C:\Database\production.mdb
Add quotes around the path of the external database.
I would also use aliases to distinguish between the 2 instances of query1.
SELECT q1Local.* FROM query1 AS q1Local
UNION ALL
SELECT q1Remote.* FROM query1 AS q1Remote IN 'C:\Database\production.mdb'

Equivalent of the query from sql server in 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