Create a WHERE OR IN postgres - mysql

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)

Related

How to write Flexible Search for joined tables in Hybris

I am trying to get all users which don't have addresses, there is a relation between user and address tables(one to many) and the address has an owner attribute that refers to Users pk. how can I achieve that ??
SELECT {u:pk}
FROM {User AS u
LEFT JOIN Address AS a ON {u:pk}={a:owner}}
WHERE {u:owner} IS NULL
I wrote a query which I guess doesn't work correctly
You can try something like this :
SELECT {pk} FROM {User} WHERE {pk} not in ({{ SELECT {owner} FROM {Address} WHERE {owner} IS NOT NULL }})

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;

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.

How to get result with variable in where condition which was set using SET

I have problem in getting the result for below query:
SELECT setting_value FROM system_settings WHERE setting_key='hold_series' INTO #Current;
where 'setting_value' is a text data type and has the value as
50472949,15804527,13613881,13607299,16226328,15774405,14416070.
SELECT * from users where Id in (#Current) [and some left joins];
resulted only one record, I am sure I have all the records in users table.
If I use
SELECT * from users where Id in (50472949,15804527,13613881,13607299,16226328,15774405,14416070)
with left joins , it resulted with all 7 rows. :(
Please let me know what am I doing wrong...
Thanks,
-Jyo
Try to use FIND_IN_SET
SELECT * from users where (FIND_IN_SET(id,#Current)>0)

Select name of creator and editor from user table

How I can select name of creator and editor from users table, creator and editor are different ids in same table, but user table is different table
Is it what you mean?
$sql = "
SELECT id,name
FROM users
WHERE users.id = editors_table.$editor_id
OR users.id = creators_table.$creator_id";
from what i understand, are you saying you have 3 tables - 1 with creator data, 1 with editor data, and a third that references a record in each of the tables using an id?
if so, you'll have to use JOINs to achieve what you want - something like:
SELECT id, name, editors_table.editor_id, creators_table.creator_id
FROM users
LEFT JOIN editors_table ON user.editor_id = editor_table.editor_id
LEFT JOIN creators_table ON user.creator_id = creator_table.creator_id
WHERE editor_table.editor_id = $editor_id_var
OR creator_table.creator_id = $creator_id_var
(you'll want to go through the query as I'm guessing here)