NOT EXISTS Clause - mysql

I have a Table called "contas" and another table called "cartoes" I need to verify what "IDCARTAO" doesn't exists in table contas, like that: "If I have one conta with cartoes.IDCARTAO = 1, the result needs to be 2 and 3";
SELECT cartoes.IDCARTAO
from cartoes
WHERE NOT EXISTS(SELECT *
from cartoes
LEFT OUTER JOIN contas ON (cartoes.IDCARTAO = contas.IDCARTAO)
WHERE contas.IDCARTAO = cartoes.IDCARTAO)
Why this sql code doesn't work?

Are you looking for this?
SELECT IDCARTAO
FROM cartoes c
WHERE NOT EXISTS
(
SELECT *
FROM contas
WHERE IDCARTAO = c.IDCARTAO
)

Tiny tweak: Instead of using not exists, try not in. As in...
SELECT ct.IDCARTAO
from cartoes ct
WHERE ct.idcartao not in
(SELECT c.idcartao from contas c)

I don't think that's how it works.
In your case, that query would return all records from the cartoes table where there are no records in the cartas table for the given IDCARTAO.
You already have 3 options up there, If you want to filter records with some specific IDCARTAO(means if you have a static list of IDCARTAO), use in. Otherwise I would use the answer from #peterm, because it would be faster when the subquery results is large.
for more reference, please hit EXISTS Condition

Related

MySQL Select from table return all, plus one column with condition and another query

I'm pretty basic at MySQL queries. I work on a Laravel web app, at this point it comes to edit the User Role's part. I have a table that lists options for a user's role (called mst_level), and another table reflects the list of users who has that role (called mst_user_level). Been search this topic several times, but always found a different case.
mst_level
mst_user_level
Expected Output:
Select all levels for a spesific user_id, and return all columns
in mst_level + 1 column called "checked", with the
condition if the user has that role in mst_user_level, return true,
else return false.
This is what I already did, which I know it's wrong
select a.*, coalesce(true, false) as checked from my_db_name.mst_level a
inner join my_db_name.mst_user_status b on a.id = b.mst_level_id
where b.mst_user_id = '363fdeea-5330-490a-b4db-34e32a3526d6'
Anyone can help me out with this? So much Appreciated...
You can do it with EXISTS:
SELECT m.*,
EXISTS (
SELECT 1
FROM my_db_name.mst_user_status s
WHERE s.mst_level_id = m.id
AND s.mst_user_id = '363fdeea-5330-490a-b4db-34e32a3526d6'
) AS checked
FROM my_db_name.mst_level m;
Or with a LEFT JOIN where the conditions are set in the ON clause:
SELECT m.*, s.mst_level_id IS NOT NULL AS checked
FROM my_db_name.mst_level m LEFT JOIN my_db_name.mst_user_status s
ON s.mst_level_id = m.id AND s.mst_user_id = '363fdeea-5330-490a-b4db-34e32a3526d6';

SQL make a select in a select

I need help with a complex select statement in SQL. I have these two table here:
Table user:
Table contacts_from_user:
When I make a select
SELECT name, vorname, gebdat, bezeichnung, wert
FROM user
JOIN contacts ON u_id = user_u_id
I get multiple lines for one user because he has more then one contact options but I need to put it in just one line:
The line should be looks like this:
name, vorname, gebdat, bezeichung_1, wert_1, bezeichnung_2, wert_2.......
How ca I do this?
Thanks a lot!
In pseudo-code, the best way to handle such scenarios is:
query = SELECT A.ID, A.stuff, B.stuff FROM A JOIN B ON A.ID = B.A_ID
results = run query
prev_A_ID = impossible_A_ID
for each result
if prev_A_ID not equal result_A_ID
create new A and set as current A
add B.stuff to current A
set prev_A_ID to result_A_ID
end for

Query table based on input parameter

I have an input parameter to a query I'm trying to write. Basically, if mostRecentSnapshot == true then I want to select only the most recent records from the process run (basically where max(creationDate)) and if mostRecentSnapshot == false then select creationDate and other columns normally.
To me, it makes sense to do this if statement in the from clause, but I don't think that's possible. Normally I would use a CTE, but I those don't exist in MySQL.
What is the best way to achieve this?
It would be something along the lines of this:
SELECT
CASE mostRecentSnapshot WHEN FALSE THEN
(
processauditheader.creationDate as processCreationDate,
processauditheader.processName,
processauditheader.processType,
processauditheader.processHost,
processauditheader.processDatabase,
processauditheader.tableAudited,
processauditheader.processInvokedByName,
processauditheader.processInvokedByType,
processauditheader.processInvokedByDatabase,
processauditheader.processIntervalValue,
processauditheader.processIntervalField,
processauditheader.auditScenarios,
processaudititerationdetail.creationDate as iterationDate,
processaudititerationdetail.connectionId,
processaudititerationdetail.processDate,
processaudititerationdetail.tableRowCount,
processaudititerationdetail.tableRowCountLastDay,
processaudititerationdetail.previousProcessAuditIterationDetailID,
processauditmetricdetail.creationDate,
processauditmetricdetail.processAuditIterationDetailID,
processauditmetricdetail.auditMetric,
processauditmetricdetail.auditTotal,
processauditmetricdetail.auditExample
)
WHEN TRUE THEN
(
((SELECT MAX(processaudititerationdetail.creationDate) as maxSnapshot,
processaudititerationdetail.id
from reporting_audit.processaudititerationdetail
group by processaudititerationdetail.creationDate) mostRecent
JOIN reporting_audit.processaudititerationdetail ON mostRecent.id = processaudititerationdetail.id)
)
END
FROM reporting_audit.processauditheader
JOIN processaudititerationdetail ON processaudititerationdetail.processAuditHeaderID = processauditheader.id
LEFT JOIN processauditmetricdetail ON processauditmetricdetail.processAuditIterationDetailID = processaudititerationdetail.id
A query can only return a fixed set of columns. Perhaps the following does what you want:
select paid.*
from reporting_audit.processaudititerationdetail paid
where (not v_mostRecentSnapshot) or
paid.creation_date = (select max(paid2.creationDate from reporting_audit.processaudititerationdetail paid2);
It will either select all records from the table or only the record(s) that have the most recent creationDate.

PostgreSQL find locks including the table name

I'm trying to take a look at locks that are happening on
specific tables in my PostgreSQL database.
I see there's a table called pg_locks
select * from pg_locks;
Which seems to give me a bunch of columns but is it possible
to find the relation because I see one of the columns is
the relation oid.
What table must I link that to to get the relation name?
This is Remy's query, adjusted for Postgres 10:
select nspname, relname, l.*
from pg_locks l
join pg_class c on (relation = c.oid)
join pg_namespace nsp on (c.relnamespace = nsp.oid)
where pid in (select pid
from pg_stat_activity
where datname = current_database()
and query != current_query());
If you just want the contents of pg_locks but with a human-friendly relation name,
select relation::regclass, * from pg_locks;
Try this :
select nspname,relname,l.* from pg_locks l join pg_class c on
(relation=c.oid) join pg_namespace nsp on (c.relnamespace=nsp.oid) where
pid in (select procpid from pg_stat_activity where
datname=current_database() and current_query!=current_query())
Remy Baron's answer is correct I just wanted to post one I came up
with as well only because it's more specific to what I need in this case
select pg_class.relname,
pg_locks.mode
from pg_class,
pg_locks
where pg_class.oid = pg_locks.relation
and pg_class.relnamespace >= 2200
;
The below command will give the list of locks:
select t.relname,l.locktype,page,virtualtransaction,pid,mode,granted
from pg_locks l, pg_stat_all_tables t where l.relation=t.relid
order by relation asc;

Why is this MySQL statement pulling data from both tables on this JOIN?

select * from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
I want it to pull everything from user_levels and nothing from collectors_users. But it's pulling from both. How do I correct the statement?
Instead of select * specify what you actually want and use select user_levels.* or even better skip the * and write out the columns you want (and consider using aliases to keep it short and tidy): select ul.col1, ul.col2 ... from userlevels ul join ...
It is getting all the data as the '*' means 'all' columns. You can limit the columns for just one table by specifying the table:
select user_levels.*
from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
Pro tip: Don't use SELECT * in running software. Instead, be as specific as you can be about the columns you want in your result set.
SELECT user_levels.*
should help a bit.
I might suggest that you use in or exists, because this is more consistent with the intention of the query:
select ul.*
from user_levels ul
where ul.id in (select cu.user_level
from collectors_users cu
where cu.username = 'testuser'
);
In addition, this version will not produce duplicate rows if collectors_users has multiple matching rows for a singel row in user_levels.
Also note the use of table aliases: these make the query easier to write and to read.