SQL 2 Tables join id - mysql

I think this is an easy question but i can't find a answer I don't have much time left...
help me please
I have a Table "Anime" with the columns "animeID, Name, ..." and I have the Table "anime_user2" with the columns "anime_userID, ..., users_userID, anime_idAnime(Foreign Key)"
Now I want to get all Names from Table "Anime" that are not in already in the Table "anime_user2" with the ID from the user that is logged in
I tried this but it doesn't work as I wanted :
"SELECT Name FROM Anime LEFT JOIN anime_user2 ON idAnime = anime_idAnime WHERE users_userID = $userID AND idAnime != anime_idAnime"
There is probably an easier way to solve this but I can't see it?

You can do as
select
a.Name
from
Anime a
left join anime_user2 au
on au.anime_idAnime = a.animeID
where
au.anime_idAnime is NULL
Here is a demo how it works

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

Combining EXISTS and LEFT JOIN

I need some help combining two queries I wrote( I do not know if it is possible to do it or not). But first let me show you the table and exaplin it so there are no ambiguous angles here.
This is the table I have (PS : I do not know how to make decent looking tables in StackOverflow even though I researched it, and tried to use Senseful solutions so please excuse the image) :
Main Table
The first query I have is the following :
SELECT *
FROM Dropship As t1
WHERE t1.HUB_SO_GOODS_ISSUE_DATE IS NULL
AND EXISTS (SELECT * FROM Dropship t2
WHERE t2.LE_PO = t1.LE_PO
AND t2.HUB_SO_GOODS_ISSUE_DATE IS NOT NULL);
This query gives me all orders that have not have been fully processed. So with the table I have it gives me the orders (LE_PO) 300 and 500 like in the following image :
result from first query
Another query I use is the left join one :
SELECT Dropship.*, Notes_Replenishment.*
FROM Dropship LEFT JOIN Notes_Replenishment ON Dropship.LE_PO = Notes_Replenishment.LE_PO;
The notes_replenishment table has all the orders (LE_PO) but also comments put in by a user. What I would like to do is to incorporate the left join in to the first query so that it gives me the result (see above) but also the comments from the Notes_replenishment table however I get errors when I tried doing it by myself.
Could somebody give me some pointers on how to combine the two queries?
Thank you all in advance!
SELECT *
FROM Dropship As t1
LEFT JOIN
Notes_Replenishment
ON t1.LE_PO = Notes_Replenishment.LE_PO
WHERE t1.HUB_SO_GOODS_ISSUE_DATE IS NULL
AND EXISTS
(
SELECT *
FROM Dropship t2
WHERE t2.LE_PO = t1.LE_PO
AND t2.HUB_SO_GOODS_ISSUE_DATE IS NOT NULL
)

ID lost after left join on table

Hey I tried this thread but it doesn't work and i can't figure out why...
here's my SQL:
SELECT * FROM gone_items
LEFT JOIN items
ON gone_items.item_ID=items.ID
WHERE
gone_items.aus_ID='$ID'
ORDER BY items.name ASC
Now, I fetch that via PHP and have a $row and try another mysql to get the individual ID's of the gone_items table. But if i use $row['ID'] I get the ID of the items.ID not the one from gone_items.ID.
I tried setting the variable manually in the first query but it doesn't work.
I also tried this: MYSQL Left join A.table and b.table while retaining a.table id
Also didn't help me...
All I want is to retain the ID (Primary key) from the gone_items table..
Can anyone please tell me what I'm doing wrong ?
Love
Gram
EDIT
//Query for Joined infos
$sqlx="SELECT foto_res_ausgeliehene_geg.ID, foto_res_ausgeliehene_geg.aus_ID, foto_res_ausgeliehene_geg.geg_ID, foto_res_ausgeliehene_geg.zusaetzliches, foto_res_gegenstaende.ID, foto_res_gegenstaende.bezeichnung, foto_res_gegenstaende.seriennummer, foto_res_gegenstaende.interne_seriennummer, foto_res_gegenstaende.zusaetzliches FROM foto_res_ausgeliehene_geg
LEFT JOIN foto_res_gegenstaende
ON foto_res_ausgeliehene_geg.geg_ID=foto_res_gegenstaende.ID
WHERE
foto_res_ausgeliehene_geg.aus_ID='$ID'
ORDER BY foto_res_gegenstaende.bezeichnung ASC
";
$ergebnisx = mysqli_query($db,$sqlx);
while ($zeilex = mysqli_fetch_assoc($ergebnisx))
{
//Query for individual infos
$sqly="SELECT * FROM foto_res_ausgeliehene_geg
WHERE `geg_ID`='".$zeilex['ID']."'
AND `aus_ID`='$ID'
GROUP BY `geg_ID`
";
$ergebnisy = mysqli_query($db,$sqly);
while ($zeiley = mysqli_fetch_assoc($ergebnisy))
{};
Now I did select all items individually. The foto_res_ausgeliehene_geg.ID still merges with the foto_res_gegenstanede.ID due to the LEFT JOIN.
So if i access $zeilex['ID'] im getting the ID of foto_res_gegenstaende.ID.
Would it help if I rename the ID field in one of the tables into lets say item_ID ?
Thanks alot.
Love
Gram.
Instead of using select *, you should explicitly state what items you want to select. Else you can get conflicts with multiple id fields. In your case something like:
select gone_items.id, gone_items.column1, gone_items.column2, items.column1, items.column2
It is also considered good practice, to limit the amount of data there is being selected. But is meanwhile also a highly debateable what is the right way. Performance issue in using SELECT *?
WORKS!
I simply renamed one of the Primary ID keys to something else, in this case, one of them got ID -> item_ID. The other one still is ID that way the left join won't merge them.
yolo
EDIT
WORKING CODE
$sqlx="SELECT foto_res_ausgeliehene_geg.item_ID, foto_res_ausgeliehene_geg.aus_ID, foto_res_ausgeliehene_geg.geg_ID, foto_res_ausgeliehene_geg.zusaetzliches, foto_res_gegenstaende.ID, foto_res_gegenstaende.bezeichnung, foto_res_gegenstaende.seriennummer, foto_res_gegenstaende.interne_seriennummer, foto_res_gegenstaende.zusaetzliches FROM foto_res_ausgeliehene_geg
LEFT JOIN foto_res_gegenstaende
ON foto_res_ausgeliehene_geg.geg_ID=foto_res_gegenstaende.ID
WHERE
foto_res_ausgeliehene_geg.aus_ID='$ID'
ORDER BY foto_res_gegenstaende.bezeichnung ASC
";
$ergebnisx = mysqli_query($db,$sqlx);
while ($zeilex = mysqli_fetch_assoc($ergebnisx))
{
//Query for individual infos
$sqly="SELECT * FROM foto_res_ausgeliehene_geg
WHERE `item_ID`='".$zeilex['item_ID']."'
";
$ergebnisy = mysqli_query($db,$sqly);
while ($zeiley = mysqli_fetch_assoc($ergebnisy))
{

CONCAT search in columns

SELECT
People.Name, People.Secondname
CONCAT(People.Name," ", People.Secondname)
FROM People, Shop, Circus
WHERE CONCAT(People.Name," ", People.Secondname) != Shop.Buyer
AND CONCAT(People.Name," ", People.Secondname) != Circus.Watcher
Ok, so this is my question. There are 2 columns. I have concat'ed them and want to find list of people who havent been in Shop AND Circus. Like: I have concated full name - "Jhon Jhonson". He havent been in circus and havent been in shop. So i want him to be shown.
Becouse I cant publish picture here, at least i can get link of table i am trying to make... http://imm.io/1k5tJ
I hope you can decipher what I want to say.
try
SELECT
People.Name, People.Secondname
CONCAT(People.Name," ", People.Secondname)
FROM People left outer join Shop on CONCAT(People.Name," ", People.Secondname) = Shop.Buyer
left outer join Circus on CONCAT(People.Name," ", People.Secondname) = Circus.Watcher
where Shop.Buyer is null and Circus.Watcher is null
Based on your comment above of: "i would like to find whom are in Concated column, but not in the Users AND not in Participants", then this query will let you choose all tbl1 data where the user AND participant do not exist in the secondary and tertiary tables respectively.
select
tbl1.name,
tbl1.name2
from People tbl1
left join Show1 tbl2 on tbl1.name = tbl2.user
left join Show2 tbl3 on tbl1.name2 = tbl3.participant
where tbl2.user is null
and tbl3.participant is null
I think your description of the problem is still unclear, but this is a step in the right direction.
Update: Change column names based on your comments on question.

mysql query 3 tables

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 :)