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
Related
New to doing SQL stuff so please excuse me.
I want to create a SQL query that joins a column to table A from table B based on the following match logic:
B.Source = ‘SOURCE1’ and A.NameCode= B.Code
If the above return NULL then I’d like to match on:
B.Source <> ‘SOURCE1’ and A.UEN = B.UEN**
Any help on how to structure this?
I currently have a union all select query that can get the values based on the above conditions. Should I be using an If/or/case_when in the join process?
A few questions in which I thought could be helpful and that I've looked at are:
How to perform a LEFT JOIN in SQL Server between two SELECT statements?
Using IS NULL or IS NOT NULL on join conditions - Theory question
But I was not able to come up with anything :(
Thank you so much!
Try something like this:
SELECT *
FROM A
JOIN B ON (
(B.Source = 'Source1' AND A.NameCode = B.Code) OR
(B.Source <> 'Source1' AND A.UEN = B.[UEN**]) --Not sure what the ** is? Part of the field name?
)
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
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
i have again bumped into an assignment that i have some question to, and yes i have been researching and reading, so this aint my first to look for answers.
the assignment is to get an output from several tables, meaning that a "join" is required, since the informations are scattered in different tables.
i have created the code that looks like this:
SELECT * FROM
order_, orderspec
WHERE order_.orderno = orderspec.orderno;
SELECT * FROM
order_, customer
WHERE order_.custno = customer.custno;
SELECT * FROM
order_, employee
WHERE order_.empno = employee.empno;
SELECT * FROM
orderspec, stock
WHERE orderspec.stockno = stock.stockno;
Is it possible to do all this in one query/ command?
You may try like this:-
SELECT * FROM
order_, orderspec, customer, employee, stock
WHERE order_.orderno = orderspec.orderno
and order_.custno = customer.custno
and order_.empno = employee.empno
and orderspec.stockno = stock.stockno;
I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"