mysql query 3 tables - mysql

I'm messing around with a mysql query over 3 tables and and I just can't get it work.
The situation: I have 3 tables.
Now I try to make a mysql query based on an inputfield, where I put in a "oxid" from the table "oxarticles" and the result should be, that I get all the articles from the category where the article/oxid is which I put in the inputfield.
Example: I put "oxid" 2 in a inputfield and press submit and the result should looks like this:
Lenkrad
Reifen
Sitz
I tried a lot but never come close to. I made a day before another query that show me all the categories based on a article but I cant modified that and used it for my actual problem.
I hope you can help me :)
Thats what I get so far, but I think this is not even close cause I get a white page.
$result = mysql_query("SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatnid IN (SELECT oxcatnid FROM oxobject2category WHERE oxobjectid = 2)")
or die(mysql_error()); ;

Now i got it:
The follow querys work:
SELECT DISTINCT oxtitle FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatind IN (SELECT oxcatind FROM oxobject2category WHERE oxobjectid = 2
and this one also:
select distinct
a.oxtitle
from
oxarticles a,
oxobject2category oc
where
a.oxid = oc.oxobjectid and
oxcatind in (select oxcatind from oxobject2category where oxobjectid=2
Thank you for the help :)

Related

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

Mysql Select with select result in an other table

I need some help, I don't know correct term of what im trying to do so im not able to find any example of what im trying to acheive.
Im hopping someone on here could just point me how how i could do this simple query and il be able to create one on my own.
SELECT * FROM
`product_views` pv,`product_sales` ps
WHERE
pv.`SessionId` = ps.`SessionId` {result of} ps.`ProductId` = 1
The syntax im trying to figure out is basacly how to get result of a query in a where clause.
Thanks!
Edit.
I realize that the fact that I don't know correct terms of what im trying to achieve is confusing so il show how in this point of time I would get the same result but in two query
SELECT ps.`SessionId` FROM `product_sales` ps WHERE ps.`ProductId` = 1;
Then with that the result of that query I would have let say sessionId 20 ( to keep it simple )
And then i would have to re-query with
SELECT * FROM `product_views` pv WHERE pv.SessionId = {ValueOfLastQuery};
A join would help you achieve what you're looking for. Here's a great introduction.
Basically, you'd want to do something like:
SELECT * FROM
product_views
LEFT JOIN product_sales
ON product_views.SessionId = product_sales.SessionId
WHERE product_sales.ProductId = '1'
Edit:
I think you could also use a subquery, which would look something like:
SELECT * FROM
product_views
WHERE SessionId = (SELECT SessionId from product_sales WHERE ProductId = '1')
How about a subquery:
SELECT * FROM `product_views` pv
WHERE pv.SessionId IN
(SELECT ps.`SessionId` FROM `product_sales` ps WHERE ps.`ProductId` = 1)

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 select then update / joining?

I have been reading for a few hours but my learning curve just isn't helping! I'm trying to find a few rows by doing a select statement, then when it matches, I need to grab the result and pair it up with another table then do an update. Somehow, from what I'm reading and applying, it's not helping me much.
Please kindly help me as I can't comprehend these things without seeing and applying what I'm doing... Here is my code:
select code as codea from routes where r1=1 (update plans set active=1 where code=codea) limit 100
You can update with JOIN like so:
UPDATE plans p
INNER JOIN routes r ON p.code = r.codea
SET p.active = 1
WHERE r.r1 = 1
LIMIT 100
Is this what you need?
update plans set
active = 1
where code = (select code as codea
from routes
where r1=1)

Select column or null?

Can I select like this?
SELECT DISTINCT idClient, idAcc,Description
FROM client, account
WHERE (account.idCliente = client.idCliente
OR account.idCliente is NULL )
Im getting trouble because it show to me duplicated results :x How can I do it ?
Thanks
EDIT:
RESULTS
idClient idAcc Description
1 3 good
1 2 bad
1 3 bad
Note that im getting 2 diferent Descriptions for same idAcc
EDIT2:
I realy need that search by NULL or Not NULL.
You are using an implicit join. Try using an explicit JOIN like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON account.idCliente = client.idCliente
I would rewrite your query to use a LEFT JOIN so that you get all situations where there is a client.idCliente, but also those where the account.idCliente doesn't exist.
Like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON client.idCliente = account.idCliente;