473,887 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Negative SQL Query

Problem
-------------

I want to return all URL records from 'fett_url' that are not currently
indexed in the lookup table 'fett_url_to_da ta' where 'data_id=2'.

i.e.

fields=> url_id, url_title, url_link, url_description

"1","Flickr","h ttp://flickr.com","Ph oto tool"

Previously I have managed to make a Positive SQL Query (see below)
when I try to reverse this including "!=" the results are not what I
require (See Negative SQL Query).

Any ideas would be greatly appreciated.
Project Fett Data Stucture
-----------------------------------------

table=> fett_url
fields=> url_id, url_title, url_link, url_description

"1","Flickr","h ttp://flickr.com","Ph oto tool"
"2","Google","h ttp://google.co.uk"," Search Engine"
"3","Yahoo","ht tp://yahoo.co.uk","W eb Portal"

lookup table=> fett_url_to_dat a
fields=> url_id, data_id

"2","1"
"2","2"
"2","6"
"3","2"
"3","5"
"3","6"
Positive SQL Query
------------------------------

Query:

SELECT *
FROM fett_url_to_dat a
LEFT JOIN fett_url
ON fett_url_to_dat a.url_id = fett_url.url_id
WHERE fett_url_to_dat a.data_id = 2
ORDER BY url_title

Returns:

fields=> url_id, data_id, url_id, url_title, url_link, url_description

"2","2","2","Go ogle","http://google.co.uk"," Search Engine"
"3","2","3","Ya hoo","http://yahoo.co.uk","W eb Portal"
Negative SQL Query
------------------------------

Query:

SELECT *
FROM fett_url_to_dat a
LEFT JOIN fett_url
ON fett_url_to_dat a.url_id != fett_url.url_id
WHERE fett_url_to_dat a.data_id = 2
ORDER BY url_title

Returns:

fields=> url_id, data_id, url_id, url_title, url_link, url_description

"2","2","1","Fl ickr","http://flickr.com","Ph oto tool"
"3","2","1","Fl ickr","http://flickr.com","Ph oto tool"
"3","2","2","Go ogle","http://google.co.uk"," Search Engine"
"2","2","3","Ya hoo","http://yahoo.co.uk","W eb Portal"

Mar 4 '06 #1
3 1895
abighill wrote:
Problem
-------------

I want to return all URL records from 'fett_url' that are not currently
indexed in the lookup table 'fett_url_to_da ta' where 'data_id=2'.

i.e.

fields=> url_id, url_title, url_link, url_description

"1","Flickr","h ttp://flickr.com","Ph oto tool"

Previously I have managed to make a Positive SQL Query (see below)
when I try to reverse this including "!=" the results are not what I
require (See Negative SQL Query).

Any ideas would be greatly appreciated.
Project Fett Data Stucture
-----------------------------------------

table=> fett_url
fields=> url_id, url_title, url_link, url_description

"1","Flickr","h ttp://flickr.com","Ph oto tool"
"2","Google","h ttp://google.co.uk"," Search Engine"
"3","Yahoo","ht tp://yahoo.co.uk","W eb Portal"

lookup table=> fett_url_to_dat a
fields=> url_id, data_id

"2","1"
"2","2"
"2","6"
"3","2"
"3","5"
"3","6"
Positive SQL Query
------------------------------

Query:

SELECT *
FROM fett_url_to_dat a
LEFT JOIN fett_url
ON fett_url_to_dat a.url_id = fett_url.url_id
WHERE fett_url_to_dat a.data_id = 2
ORDER BY url_title

Returns:

fields=> url_id, data_id, url_id, url_title, url_link, url_description

"2","2","2","Go ogle","http://google.co.uk"," Search Engine"
"3","2","3","Ya hoo","http://yahoo.co.uk","W eb Portal"
Negative SQL Query
------------------------------

Query:

SELECT *
FROM fett_url_to_dat a
LEFT JOIN fett_url
ON fett_url_to_dat a.url_id != fett_url.url_id
WHERE fett_url_to_dat a.data_id = 2
ORDER BY url_title

Returns:

fields=> url_id, data_id, url_id, url_title, url_link, url_description

"2","2","1","Fl ickr","http://flickr.com","Ph oto tool"
"3","2","1","Fl ickr","http://flickr.com","Ph oto tool"
"3","2","2","Go ogle","http://google.co.uk"," Search Engine"
"2","2","3","Ya hoo","http://yahoo.co.uk","W eb Portal"

SELECT a.url_id,
a.url_title,
a.url_link,
a.url_descripti on,
b.url_id,
b.data_id
FROM fett_url a
LEFT OUTER JOIN fett_url_to_dat a b
ON a.url_id = b.url_id
WHERE a.data_id = 2
and b.url_id is null
ORDER BY a.url_title

Because you are doing a left-outer join, if it does not exist in B
(fet_url_to_dat a) it should return NULL.

for consistency in all SQL-compliant databases, you will want to list
each field, unless you are doing a single table select. - for
simplicity, I always use an abbreviated table alias.
Mar 5 '06 #2
Hi noone,

I tried your suggested SQL query and phpMyAdmin returned:

MySQL said:
#1054 - Unknown column 'a.data_id' in 'where clause'

This is because the 'data_id' does not exist in 'fett_url a' so I
changed the SQL query to read:

SELECT a.url_id,
a.url_title,
a.url_link,
a.url_descripti on,
b.url_id,
b.data_id
FROM fett_url a
LEFT OUTER JOIN fett_url_to_dat a b
ON a.url_id = b.url_id
WHERE b.data_id = 2
and b.url_id is null
ORDER BY a.url_title

This time the query ran successfully but nothing was returned.

Mar 5 '06 #3
I posted the same probelm on the PHP Freaks Forum and Barand kindly
solved the problem for me:

SELECT u.*
FROM fett_url u
LEFT JOIN fett_url_to_dat a d
ON d.url_id = u.url_id AND d.data_id = 2
WHERE d.url_id IS NULL
ORDER BY u.url_title

Thanks everyone!

See post here: http://www.phpfreaks.com/forums/inde...howtopic=87476

Mar 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8233
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any negative numbers being hidden from me :( I tried checking Tools/Options/Connections/Use Regional Settings both on and off, stopping and restarting M$ Query Analyer in betwixt, but no improvement.
3
5685
by: Eveline | last post by:
Dear all, I have a database on which I do stuff like: - creating tables - filling those tables with data - joining tables - EXPORT of queries to file - LOAD data from file into a tables
12
28988
by: deko | last post by:
Is there an easy way to make all negative values positive in a particular table? I've been experimenting with this: Dim rst As DAO.Recordset Set rst = db.OpenRecordset("tblNegValues") Do If rst!Amount < 0 Then rst!Amount = rst!Amount - (rst!Amount * 2)
3
3182
by: Tripp Knightly | last post by:
I have a lookup table from which I want to categorize various bands of customer net income. Some of the income is positive, some is negative. The bands vary in size (ie, <500, -200 to 0, 100 to 1000). When I have a lookup table w/ the "threshold" amounts of income, I'm not able to get dlookup to work, and I'm pretty sure it's getting tripped up by negative / positive lookup values. Is it not possible to do lookups on a table w/ both...
1
2350
by: Kevin | last post by:
Group By - Client Sum - PastDue: Iif(DateDiff("d",,Date())>30,,0) Sum - Current: Iif(DateDiff("d",,Date())<31,,0) These 2 sum fields work fine, EXCEPT it is not summing my negative numbers/credits. Can not understand why?
7
5391
by: hasanainf | last post by:
Hi all, I have two querys QueryPurchased ProductID Location TotPcs Prod1 Loc1 100 Prod2 Loc1 50 Prod3 Loc1 150 Prod3 Loc3 150
15
35539
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
4040
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer, and that it contains no padding. Firstly, I realise that the MSB is known as the sign-bit, and that it indicates whether the number is positive or negative (irrespective of which negative number system is used).
10
1774
by: mirandacascade | last post by:
Question toward the bottom of this post....background information immediately below. Access 97 SQL Server 2000 Please note: although the subject line uses the word 'bloat', this post is NOT a "what can I do to prevent bloat?" inquiry. When I searched the postings in this group for information about bloat, I believe I gained a rudimentary understanding of some of the things
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10877
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.