useful SQL queries

Posted by Zu on May 13, 2010 in SQL |

Find all users that are in tblUser table  but are not in tblTransaction (very useful if there’s a foreign key issue)

SELECT USER_ID FROM tblTransaction WHERE USER_ID NOT IN (SELECT ID FROM TBLUSER)

Now this one is used when you have a table linking a many to many relationship.  This query pulls all currently assigned individuals and the event countries they are assigned to:

SELECT FIRST_NAME, COUNTRY
FROM tblEventContact t1, tblContact t2, tblEvent t3
WHERE t2.id = t1.contact_id
AND t3.id = t1.event_id

SELECT FIRST_NAME, COUNTRYFROM tblEventContact t1, tblContact t2, tblEvent t3where t2.id = t1.contact_idand t3.id = t1.event_id

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2012 Cheat Sheet All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.