Can't find syntax error in simple sql command - mysql

I have been looking at this for a while and i can't find the syntax error.
IF EXISTS (SELECT * FROM ElpasoTable WHERE elapsoCommand = 'shouldRunWlapon')
BEGIN
END

Try this for MySQL
IF EXISTS (SELECT * FROM ElpasoTable WHERE elapsoCommand = 'shouldRunWlapon') THEN
---- YOUR CODE HERE
END IF;

Valid commands..
DROP TABLE IF EXISTS...,CREATE TABLE IF NOT EXISTS..
You cannot use IF EXISTS simply in a mysql command.
for your need you can try this instead
perfect solution for your case
usage of IF EXISTS

Actually I don't know what output u want but here is the suggestion.
We can't use directly if exists statement.
We can use it after certain commands such as delete ,update ,Create etc.
First if u want the desired output,remove if exists.
So the query will be ( SELECT * FROM ElpasoTable WHERE elapsoCommand = 'shouldRunWlapon')
If ur table has column named elapsocommand and value is shouldRunWlapon,,then the Elapsocommand table is displayed.
U can use some concepts between begin and end statements based on ur requirement.
They are stored procedures , triggers, views etc

Related

Is there a way to alias a table or database in more than one query

I need to write a sequence of queries where I want to sometimes use a different table or database and don't want to keep writing them over and over again which can lead to a mistake.
SET #NAME = "Fred";
SELECT pid FROM old_table.people WHERE name = #NAME INTO #PID;
INSERT new_table.address SELECT *
FROM old_table.address o WHERE o.pid = #PID;
...
Is there a way todo a global SET or ALIAS so I don't have to repeat the database.table names again and again with the chance of a typo.
ie looking for something like this - this is not real sql
ALIAS old_table.people #OP;
ALIAS new_table.people #NP;
ALIAS old_table.address #OA;
ALIAS new_table.address #NA;
SET #NAME = "Fred";
SELECT pid FROM #OP WHERE name = #NAME INTO #PID;
INSERT #NA SELECT * FROM #OA o WHERE o.pid = #PID;
The closest thing to what you ask for is a VIEW. But I don't see how this would help avoid typos. You'd just be swapping one name for another name. You could make a typo on the view name just as easily.
You definitely cannot use a user-defined variable as a table identifier. When you use a variable in a query, it is as if you used a string value.

SELECT * FROM db1 WHERE db1.table.value = db2.table.value

I'm working with mySQL db and trying to display the correct data for the user. In order to do that I check if the data that I call from one backend is equal to username from another backend like so
SELECT * FROM db1 WHERE db1.table.value = db2.table.value
Names of databases are A and B.
SELECT *
FROM `A.onboardings`
, `B.loginsystem`
WHERE onboardings.sales_email = loginsystem.username
The problem is I get an error A.A.onboardings doesn't exists and A.B.loginsystem doesn't exist pls help :(
You must use this form - from A onboardings
You have to put the backticks in the right pace, or else mysql things your table is called A.onboardings
As seen bleow the needs to be around the database and the table name
And the use of aliases helps to keep even in big queries a good overview and yu have to write less
"SELECT * FROM `A`.`onboardings` a1,`B`.`loginsystem` b1 WHERE a1.sales_email = b1.username"
Try this one( Change the query according to your DB name, table, and matching column name)
SELECT * FROM mydatabase1.tblUsers INNER JOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID
The problem is that
`A.onboardings`
is not the same as
A.onboardings
The first is a table reference where there table name has a period in it. The second is for the onboardings table in database A.
In addition, you should be using JOIN!!!
SELECT *
FROM A.onboardings o JOIN
B.loginsystem ls
ON o.sales_email = ls.username;
If you feel compelled to escape the identifies -- which I do not recommend -- then:
SELECT *
FROM `A`.`onboardings` o JOIN
`B`.`loginsystem` ls
ON o.sales_email = ls.username;

How to optimized my code in Mysql?

I hava three tables called t_asset,t_device and t_asset_device.The relationship between t_asset and the t_device is multiple pairs.Each table column is :
t_asset :id , asset_name,asset_code,create_time,creator
t_device:id, device_name,device_code,latitude,longitude,create_time,creator
t_assets_device:id,asset_id,device_id,create_time,creator
Now I want to get all the t_asset and the latitude,longitude of the first device,So I write the code and function like these:
fun_getLatitudeByAssetId(`assetId` varchar(50)){
BEGIN
declare v_latituede DECIMAL(10,5) DEFAULT(-1) ;
select latitude into v_latituede
from t_device tDevice
inner join t_assets_device tAssetsDevice
on tAssetsDevice.asset_id=assetId and
tDevice.id=tAssetsDevice.device_id
and tDevice.latitude!=-1
ORDER BY tDevice.id desc
limit 0,1;
return v_latituede;
END
}
fun_getLongititueByAssetId(`assetId` varchar(50)){
BEGIN
declare v_longititue DECIMAL(10,5) DEFAULT(-1) ;
select longititueinto v_longititue
from t_device tDevice
inner join t_assets_device tAssetsDevice
on tAssetsDevice.asset_id=assetId and
tDevice.id=tAssetsDevice.device_id
and tDevice.latitude!=-1
ORDER BY tDevice.id desc
limit 0,1;
return v_longititue ;
END
}
The final query sql is:
select tAsset.*,fun_getLatitudeByAssetId(tAsset.id) latitude,
fun_getLongititueByAssetId(tAsset.id) longititue from t_asset tAsset
It seems that I have query the latitude and longititue two times,If I want to get the other field from the t_device,I do not want to write another function
like fun_getDeviceCodeByAssetId, How can I optimized my code?
I don't think a function or procedure is the way to go - why not just define a view that has asset_id + all the other fields you want? Then just join to it on asset_id rather than calling functions. In addition to just being cleaner, I'd be concerned about performance with row rather than set processing with the function approach (this is total speculation, I don't have deep enough knowledge of MySQL to know how it's handled)
Is it really necessary to do it with functions?
You can do it with views, for example:
create view latitudeLongitude as
select latitude,longitude,asset_id
from t_device tDevice
inner join t_assets_device tAssetsDevice
on tDevice.id=tAssetsDevice.device_id
and tDevice.latitude!=-1;
Finally your last select should look like this:
select tAsset.*,latitudeLongitude.latitude,
latitudeLongitude.longititue
from t_assettAsset inner join latitudeLongitude
on t_assettAsset.id = latitudeLongitude.asset_id
If you're trying to return several values at once then you should rather declare a stored procedure, not a function. Then you'll be able to write select latitude, longitude from ... inside your procedure and then call it with a command like call getLatAndLong(...)

Add user if not exists in another table

I need to add usernames to users_waiting table if they were not added to users table before.
var usernamesArray = ['test', 'test2'];
connection.query('REPLACE INTO users_waiting (username) VALUES ?', [usernamesArray]);
I'm using node.js mysql client and i have no idea how to do that.
You have many options of doing it.
One of which is with NOT EXISTS() :
INSERT INTO <Table2> (...)
SELECT .... FROM <Source> t
WHERE NOT EXISTS(SELECT 1 FROM <Table1> s
WHERE t.id = s.id);
I don't think it's exactly what you need. But you can figure out the rest from this.

Can't update a target table

I have two tables, one a options table (rot) that has the metrics of a set of rooms and another that has the current state of the rooms (rt). I want to have a daily event that updates the cost_to_date in the rt using the values stored in the rot.
When I tried the SQL:
UPDATE room_tbl SET COST_TO_DATE_rt = COST_TO_DATE_rt + (
SELECT PerDiem_rot FROM room_options_tbl, room_tbl
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`)
I get the error: #1093 - You can't specify target table 'room_tbl' for update in FROM clause
My searching for a solution led me to try a temporary table using aliasing but my attempts at it have all resulted in syntax errors. Any help would be appreciated.
You can't use a query because its not supported by MySQL in an update clause
From 13.2.10. UPDATE Syntax
Currently, you cannot update a table and select from the same table in a subquery.
Instead try the following
UPDATE room_options_tbl, room_tbl
SET COST_TO_DATE_rt = COST_TO_DATE_rt + PerDiem_rot
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`