sql on mysql about join - mysql

the code below provide a result too much Infact i want to list the customer that never buy somethink How can i fix the code below
SELECT
webboard.listweb.id,
webboard.listweb.iditempro,
webboard.listweb.url,
webboard.listweb.useradddate,
webboard.listweb.expiredate,
webboard.prorecord.urlpostonweb
webboard.prorecord.urlpostonweb
FROM
webboard.listweb ,
webboard.prorecord
Where listweb.id Not In
(select webboard.prorecord.idlist From webboard.prorecord )

Using the syntax
FROM
webboard.listweb ,
webboard.prorecord
will perform a cartesian, or cross, join on the tables involved. So for every row in the table listweb all the rows in prorecord are displayed.
You need to use an INNER JOIN to only select the rows in listweb that have related rows in the prorecord table. What are the fields which identify the rows (your Primary Keys) and what is the name of the foreign key field in the prorecord table?
EDIT: Just re-read the question and comments and I see you want the rows in listweb which do not have an entry in prorecord
Your SELECT will then look like:
SELECT
webboard.listweb.id,
webboard.listweb.iditempro,
webboard.listweb.url,
webboard.listweb.useradddate,
webboard.listweb.expiredate,
webboard.prorecord.urlpostonweb
-- webboard.prorecord.urlpostonweb -- You have this field twice
FROM webboard.listweb LEFT JOIN webboard.prorecord
ON webboard.listweb.id = webboard.prorecord.idlist -- I'm guessing at the foreign key here
WHERE webboard.prorecord.idlist IS NULL

Related

how can i make a view using the foreign key that is applies in mysql?

I am still a beginner and i searched in google ,read w3schools and couldn't get how to do it the following thing.
i want to create view from the info in cases field
AS you saw these are the info inside the tables (names,depart,idcards)
this the table i want to get the data from i made all the column start with id_* as foreign key for the PK in the previous tables
note:id_case is the PK of the table, id_dep is for department,id name is for name,id_complaint
This is just a bunch of joins:
CREATE VIEW viewname AS
SELECT *
FROM cases AS c
JOIN names AS n ON c.id_name = n.id
JOIN depart AS d ON c.id_dep = d.id
JOIN complaint AS cm ON c.id_complaint = cm.id
...
Note that if there are any column names that are the same among any of the tables, you'll need to list them explicitly in the SELECT list and assign distinct aliases to them. See MySQL JOIN tables with duplicate column names

MySQL Joins. match two conditions in two columns

I am trying to fetch data from the 2 tables using joins. But I don't know how to do it. I have read about joins but it wasn't making any sense since I am new.
I have two tables.
Playgrounds Inspection
id u_id id playground_id
1 2 1 1
2 2 2 2
In playgrounds table id is the playground id and is unique and u_id is the user of the playground.
In inspection table id is unique and playground_id is used as a foreign key from playgrounds table's id.
My problem is that I have to pass id and u_id to the the query in playgrounds table then it should select id from playground table. Then it should select every thing from inspection table based on that id .
Hope I have explained it properly.
Any help is much appreciated.
Thanks in advance
JOIN operations, whether INNER JOIN, LEFT JOIN, or RIGHT JOIN, have this overall syntax.
table_expression JOIN table_expression ON boolean_expression
When the Boolean expression is true, the JOIN concatenates the matching rows of the two tables into a single row in the result.
In your case, you want the ON clause's Boolean expression to say
Playgrounds.id = Inspection.playground_id
because that's how you know a row from Inspection relates to a row in Playgrounds. Accordingly, a statement like
SELECT Inspection.id AS Inspection_ID,
Playgrounds.id AS Playground_ID,
Playgrounds.u_id AS User_ID
FROM Playgrounds
JOIN Inspection ON Playgrounds.id = Inspection.playground_id
WHERE Playgrounds.u_id = something
AND Playgrounds.id = something_else
See how it goes? Your SELECT makes a new table, sometimes called a result set, by JOINing the rows of two existing tables on the criteria you choose in the ON clause.
You can put anything you want into the ON clause. You could put
Playgrounds.id = Inspection.playground_id OR Inspection.id < 20
or
Playgrounds.id = Inspection.playground_id OR Inspection.scope = 'citywide'
for example (if you had a scope column in the Inspection table).
But, don't go there until you master the basic JOIN operation.

MySQL Delete Subsequent Duplicates

I know there is a lot of results that come up when you search this, but I can't find a simple, clear answer to what I am trying to to.
To make it simple, say I have a table with two columns:
'call_id' (pk, unique, auto increment) & 'guid'
I want to delete any rows with a duplicate guid, keeping just the first occurrence of each guid.
You can use join:
delete c
from calls c left join
(select guid, min(call_id) as minci
from calls
group by guid
) cc
on cc.minci = c.call_id
where cc.minci is null;

select from database table rows that are not in another table

I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)

SELECT Query where only one of three fields containing Foreign Keys is filled

As shown below the Table Recording contains three foreign keys, only one of which will be filled for any one entry. My question is how can I format a select statement in Access that will only pull the names from the relevant table given the foreign key for that table?
I have tried using IIf, which works when checking which Foreign Key is Not Null after the SELECT:
SELECT Recording.[idRecording],
IIf(Recording.[Artist_idArtist] Is Not Null,
Artist.[artName] ,
IIf(Recording.[Band_idBand] is Not Null,
Band.[bName],
Composer.[cName]))
FROM ...
But putting any conditions after the FROM statement, to get the correct JOIN, results in an error:
FROM
IIf(Recording.[Artist_idArtist] Is Not Null,
Artist INNER JOIN Recording ON Recording.[Artist_idArtist] = Artist.[idArtist],
IIf(Recording.Band_idBand Is Not Null,
Band INNER JOIN Recording ON Recording.[Band_idBand] = Band.[idBand],
Composer INNER JOIN Recording ON Recording.[Composer_idComposer] = Composer.[idComposer]));
I'm new to this so I'm probably missing something obvious, or going about it the wrong way. I believe what I've ended up with here is an Exclusive Arc? I'm not sure if this is even a good design, I had thought of scrapping the Recording table and simply adding the foreign keys to Track.
For reference here is the Relationship Design:
You can solve the problem with LEFT JOINs
SELECT
R.idRecording,
Nz(A.artName, Nz(B.bName, C.cName))
FROM
((Recording R
LEFT JOIN Artist A
ON R.Artist_idArtist = A.idArtist)
LEFT JOIN Band B
ON R.Band_idBand = B.idBand)
LEFT JOIN Composer C
ON R.Composer_idComposer = C.idComposer
They return all the records form the table on the left side and only the records from the table on the right side for which the join condition is met.
I also simplified the IIf cascade with the Nz function, which returns the second argument when the first one is null.
I also use short aliases for table names, thus simplifying the query further.