Add user if not exists in another table - mysql

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.

Related

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;

Can't find syntax error in simple sql command

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

Create a WHERE OR IN postgres

Right now I have the query
DELETE FROM connections
WHERE "user_b_id" IN
(SELECT "id"
FROM "users"
WHERE 'dharness.student#gmail.com'="email")
But what I really want is
WHERE "user_b_id" OR "user_a_id" IN ...
But I can't figure out how to make that work. Is there an operator for this?
If users.email is unique you can write
WHERE (SELECT id FROM users
WHERE email = 'dharness.student#gmail.com')
IN (user_b_id , user_a_id)

How to copy 3 columns from one table into another in mysql

I have two tables and both include 2 columns, sureness and kindness and and they are related to each other by dataitemID and id. Just to show you the structure I will put 2 selects that I got from database:
SELECT ID,sureness,kindness FROM omid.tweet ;
SELECT ID,DataitemID,sureness,kindness FROM omid.entity_epoch_data ;
and I want to copy all value of sureness and kindness in omid.tweet into entity_epoch_data where entity_epoch_data.entityID is equal to entityID coming from entity_relation where tweet.ID =entity_relation.ID
I want just to do it in mysql rather than reading the whole table in java and updating the database in the loop but I am so confused. How can I do that?I appreciate any help:)
Update:
I wrote the code as follow but it does not work:
update tweet, entity_epoch_data
set entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness ,
entity_epoch_data.calmness = tweet.calmness ,
entity_epoch_data.happiness = tweet.happiness
WHERE entity_epoch_data.EntityID in(
SELECT EntityID FROM omid.entity_dataitem_relation
INNER JOIN omid.tweet t ON entity_dataitem_relation.DataitemID = t.ID)
It's actually pretty straight forward. The UPDATE clause works like a JOIN and then use SET to set the values
UPDATE tweet INNER JOIN entity_epoch_data
ON tweet.id = entity_epoch_data.id
SET entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness

MySQL INSERT-SELECT a non-mandatory field with JOIN

I have a table (netStream), that has 2 foreign keys: (logSessions_logSessionID) and (accountSessions_accountSessionID).
The (logSessions_logSessionID) is mandatory the (accountSessions_accountSessionID) is NOT mandatory.
Here is the part of the block-scheme that shows the connections and the non-mandatory status:
(The background: logSessions are the sessions that every visitor have, accountSessions are the login sessions. Everybody has a logSession (since everybody is a visitor), but not everybody is logged in, so they do not have accountSession)
I want to insert a row into (netStream), in every case there is a (logSession), but it is not the same with (accountSession). So, when there is an (accountSession), I want to insert that ID too, if there is no (accountSession), then just leave that field in (netStream) NULL.
The hash values are stored in Binary(x), this is why I use UNHEX().
This is the MySQL I wrote, there is no error message, but it does not work. What is the problem?
INSERT INTO `test-db`.`netStream` (`netStreamHash`, `logSessions_logSessionID`, `accountSessions_accountSessionID`)
SELECT UNHEX("1faab"), `logSessions`.`logSessionID`, NULL FROM `logSessions` CROSS JOIN `accountSessions`
WHERE `logSessions`.`logSessionHash` = UNHEX("aac") AND
`accountSessions`.`accountSessionHash` = UNHEX("2fb");
If understand you correctly you are probably looking for something like this
INSERT INTO `test-db`.`netStream` (
`netStreamHash`,
`logSessions_logSessionID`,
`accountSessions_accountSessionID`)
SELECT UNHEX("1faab"),
(SELECT `logSessionID` FROM `logSessions`
WHERE `logSessionHash` = UNHEX("aac")),
(SELECT `accountSessionID` FROM `accountSessions`
WHERE `accountSessionHash` = UNHEX("2fb"));
If there is no matching row in accountSessions then you'll get NULL inserted in accountSessions_accountSessionID in netStream table