Jump to content

Recommended Posts

Table 1

---

uniqueID / employeeName

1 / Dave

2 / Steve

3 / Robert

 

Table 2

---

job / employeeID

"telephone Mr Smith" / 2

"complete report abc" / 1

 

Table 3

---

notes / employeeID

"various text entered here" / 2

"hello, more notes" / 2

 

 

For my sql query I need to check which employees (from table 1 and if any) DO appear in table 2, but then not in table 3. In this case this would be YES, employee 1.

Link to comment
https://forums.phpfreaks.com/topic/228571-sql-query-pretty-simple/
Share on other sites

This should do you good

 

SELECT * FROM name, job, notes WHERE name.id NOT IN (SELECT notes.employeeid FROM notes) AND name.id IN (SELECT job.employeeid FROM job)

 

EDIT: I just built a table structure exactly as you built it and it functioned perfectly. the result was Dave

or....

SELECT uniqueID, employeeName 
FROM table1 LEFT JOIN table2 ON
(table1.uniqueID = table2.employeeID)
LEFT JOIN table3 ON 
(table1.uniqueID = =table3.employeeID)
WHERE (table2.employeeID IS NOT NULL) AND (table3.employeeID IS NULL)

 

@jay7981 : SELECT * from a two field database?!?!?! REALLY??  :o

It's only got two fields, "SELECT field1, field2 FROM" is a more solid selection method, returning all the data anyway.  That said, what if he doesn't need it?  Do you pack all the clothes, books, games, furnature you own to go on holiday for a weekend "just in case"?  :P 

I haven't heard a single GOOD argument for using SELECT * (ever).

Thanks all for the help.

 

Muddy_Funster, the way you have written it is the most familiar to me as this is how I would write a normal join.

 

In fact the reason I asked the question was the complication with the fact that they must appear in table 2 but can't in table 3.

 

However, this part of your code:

WHERE (table2.employeeID IS NOT NULL) AND (table3.employeeID IS NULL)

...is the main bit that isn't too familiar to me (and is obviously the key bit about there must be a record in t2 and can't in t3).

 

Just one question if I could.

- What if there was a record in table 3 but it held an empty value, lets say:

 

notes / employeeID

"various text entered here" / 2

(empty value) / 3

"hello, more notes" / 2

 

How would I change the query to ensure it still works? (can you ensure it works if there are a few empty values as well?) e.g.:

"various text entered here" / 2

(empty value) / 3

"hello, more notes" / 2

(empty value) / 3

 

The example here is a small part of something bigger, I just wanted to use something like this to explain it. Thanks.

 

Thanks again for the help everyone.

NULL is an empty value.  Even white space takes up space, it has ascii code that represents it, so I think you are asking, what would happen in the event of white space being in there?  Then, as you suspect, there would be a result returned.  You can tweek the code to include any combination of fitering that you want.  for other conditions that you want to exclude simply add them using OR within encompasing brackets, eg:

WHERE (table2.employeeID IS NOT NULL) AND (table3.employeeID IS NULL) OR (table3.employeeID = ' ') OR (table3.employeeID NOT BETWEEN 0 AND 9999999))

just like in complex mathimatics, the brackets help dictate the execution order.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.