How to use LIKE and IN operators in one query? - sql

My simple sql statement is this:
SELECT USERNAME FROM ALL_USERS;
But I want to filter system users from the result. I searched but couldn't find anything that Oracle provided; so I tried a statement like this and it doesn't work:
select username from all_users where username not like '%SH%'
or username not like '%SYS%'
or username not in ('ANONYMOUS',
'DBSNMP',
'MGMT_VIEW',
'ORDPLUGINS',
'OUTLN',
'SI_INFORMATION_SCHEMA',
'WK_TEST',
'WKPROXY',
'XDB');
This doesn't work. How should I modify my query for the desired output or maybe there is something oracle provides to get predefined system accounts?

A slight twist on the other answers: not (A or B or C) = not A and not B and not C, so what you probably originally wanted was:
select username from all_users
where not (username like '%SH%'
or username like '%SYS%'
or username in ('ANONYMOUS',
'DBSNMP',
'MGMT_VIEW',
'ORDPLUGINS',
'OUTLN',
'SI_INFORMATION_SCHEMA',
'WK_TEST',
'WKPROXY',
'XDB')
);

Instead of OR, you will need to use AND
select username from all_users where username not like '%SH%'
AND username not like '%SYS%'
AND username not in ('ANONYMOUS',
'DBSNMP',
'MGMT_VIEW',
'ORDPLUGINS',
'OUTLN',
'SI_INFORMATION_SCHEMA',
'WK_TEST',
'WKPROXY',
'XDB');
However, this will also filter legitimate non-system users whose names contain SYS or end in SH. User JOSH will be lost.

replace or with AND , I think it will start working.
However a better approach might be to have a flag indicating whether its a system user or not. A more sophosticated approach might be to have seperate table for rights that will have many to many relationship with User table.

You can try this:
select username from all_users where username not like '%SH%'
AND username not like '%SYS%'
INTERSECT
select username from all_users where username not in ('ANONYMOUS',
'DBSNMP',
'MGMT_VIEW',
'ORDPLUGINS',
'OUTLN',
'SI_INFORMATION_SCHEMA',
'WK_TEST',
'WKPROXY',
'XDB');

Related

PostgreSQL to get data by email irrespective of the minor difference in domain name

I have a scenario where authentication is done by the 3rd party and we store the records in our database ie. postgreSQL.
For Example:
email_id
details
name
aaa124#domain.com
some details
aaa124
aaa124#ex.domain.com
some details
aaa124
What I want is to extract all the recores by email Id only irrespective of the minor differences in the domain name changes. As they treat domain.com and ex.domain.com as same.
I have tried
SELECT * FROM table where (
email_id like 'aaa124%'
and
email_id like '%domain.com'
);
but looking for some optimal solution or regex etc.
Note : A user can be registered with multiple domains as well. so need to filter by domain also.
Thanks in advance.
You can combine LIKE clauses, like this:
select * from testing
WHERE email_id LIKE 'aaa124#%domain.com';
Output:
aaa124#domain.com
aaa124#ex.domain.com
Here is a db <> fiddle in action.
Do you want split_part()?
select split_part(email, '#', 1) as email_name,
split_part(email, '#', 2) as domain
from t;
Something like
select * from yourTable where email like 'aaa124#%';
This will select all users with the email aaa124 for all domains. You may want to use ilike instead of like if you also want match different cases of the email address.

How to know currently logged in username in Oracle SQL?

How to know currently logged in username in Oracle SQL, if we are dealing with multiple users within the same DB, it is little bit confusing to understand!
By using this query we can get currently logged in user
select user from dual;
There are many different ways.
Option 1
Use the V$SESSION view.
SELECT USERNAME FROM V$SESSION;
Option 2
This one gives all the detailed information because there are times that you have to locate locked sessions.
select
substr(a.spid,1,9) pid,
substr(b.sid,1,5) sid,
substr(b.serial#,1,5) ser#,
substr(b.machine,1,6) box,
substr(b.username,1,10) username,
-- b.server,
substr(b.osuser,1,8) os_user,
substr(b.program,1,30) program
from v$session b, v$process a
where
b.paddr = a.addr
and type='USER'
order by os_user,username;

SQL SELECT where column CONTAINS substring

I have a database containing all usernames. Now, I have a page where I can search for an user. At the moment I use the following SQL:
SELECT uid, username, id, status
FROM users
WHERE `username` LIKE <search string>
Now, I have a record with the username Hattorius. But when I use that SQL syntax, and search for hatt. It doesn't give any results. How can I still make this work?
I searched some around, but nobody really had an answer to this.
Try to use LIKE :
SELECT uid, username, id, status
FROM users
WHERE `username` LIKE '%hatt%'
Remove the single quotes:
SELECT uid, username, id, status
FROM users
WHERE username LIKE '%hatt%'

Select a user by their username and then select data from another table using their UID

Sorry if that title is a bit convoluted... I'm spoiled by an ORM usually and my raw SQL skills are really poor, apparently.
I'm writing an application that links to a vBulletin forum. Users authenticate with their forum username, and the query for that is simple (selecting by username from the users table). The next half of it is more complex. There's also a subscriptions table that has a timestamp in it, but the primary key for these is a user id, not a username.
This is what I've worked out so far:
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
JOIN forum.subscriptionlog
WHERE
forum.user.username LIKE 'SomeUSER'
Unfortunately this returns the entirety of the subscriptionlog table, which makes sense because there's no username field in it. Is it possible to grab the subscriptionlog row using the userid I get from forum.user.userid, or does this need to be split into two queries?
Thanks!
The issue is that you are blindly joining the two tables. You need to specify what column they are related by.
I think you want something like:
SELECT * FROM user u
INNER JOIN subscriptionlog sl ON u.id = sl.userid
WHERE u.username LIKE 'SomeUSER'
select * from user u JOIN subscriptions s ON u.id = s.id where u.username = 'someuser'
The bit in bold is what you want to add, it combines the 2 tables into one that you return results from.
try this
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
INNER JOIN forum.subscriptionlog
ON forum.subscriptionlog.userid = forum.user.userid
WHERE
forum.user.username LIKE 'SomeUSER'

Problems with joining two tables in SQL

I have two tables. user(user_id, username, password, age) and comment(comment_id, comment, user_id(foreign key)).
I'm trying to get username from user, using the user_id provided in comment.
My query looks like this:
$sql = "SELECT username FROM user WHERE user_id = (SELECT user_id FROM comments)";
I'm getting null. Is my brain working poorly or is it something else I messed up?
I just want to display all comments after each other, with the username before it.
Use IN instead of "=" .
SELECT username FROM user WHERE user_id IN (SELECT user_id FROM comments);
OR you can use a proper join, something like:
SELECT username FROM user,comments WHERE user.user_id = comments.user_id
That's not a join - a join would be:
$sql = "SELECT username FROM user u JOIN comments c ON u.user_id = c.user_id";
When you use a subquery with =, the subquery must return one value. To show all related records in a related table, use JOIN instead.