473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql join

red
This is a simple join but I am having trouble with it. I feel like such
an idiot.

I start with this select statement, which works fine:
$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b
WHERE a.auction=b.id" ;

Now I only want the results from auctions with ids that are not listed
in the 'archived' field of the PHPAUCTIONXL_ar chived table. So I added
another table and an AND to the SELECT statement. Now I don't understand
why it doesn't show the auctions which are not archived.

$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b,
PHPAUCTIONXL_ar chived c
WHERE a.auction=b.id
AND a.auction !=c.archived";

How do I get this to work ?

red
Jul 17 '05 #1
8 2053
I noticed that Message-ID: <a4************ **********@twis ter.nyc.rr.com>
from red contained the following:
AND a.auction !=c.archived";

How do I get this to work ?


Just a guess (I'm not a major SQL guru)

AND NOT a.auction =c.archived
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
Geoff Berrow wrote:
I noticed that Message-ID: <a4************ **********@twis ter.nyc.rr.com>
from red contained the following:

AND a.auction !=c.archived";

How do I get this to work ?

Just a guess (I'm not a major SQL guru)

AND NOT a.auction =c.archived


or:
AND a.auction <> c.archived

http://dev.mysql.com/doc/mysql/en/co...operators.html
http://dev.mysql.com/doc/mysql/en/lo...operators.html

HTH
Jul 17 '05 #3
red wrote:
This is a simple join but I am having trouble with it. I feel like such
an idiot.

I start with this select statement, which works fine:
$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b
WHERE a.auction=b.id" ;

Now I only want the results from auctions with ids that are not listed
in the 'archived' field of the PHPAUCTIONXL_ar chived table. So I added
another table and an AND to the SELECT statement. Now I don't understand
why it doesn't show the auctions which are not archived.

$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b,
PHPAUCTIONXL_ar chived c
WHERE a.auction=b.id
AND a.auction !=c.archived";

How do I get this to work ?

red


http://www.w3schools.com/sql

Try it. Good site for starters. Really.

good luck!

Regards
Jul 17 '05 #4
Stijn Verholen wrote:
Geoff Berrow wrote:
I noticed that Message-ID: <a4************ **********@twis ter.nyc.rr.com>
from red contained the following:

AND a.auction !=c.archived";

How do I get this to work ?


Just a guess (I'm not a major SQL guru)

AND NOT a.auction =c.archived

or:
AND a.auction <> c.archived

http://dev.mysql.com/doc/mysql/en/co...operators.html
http://dev.mysql.com/doc/mysql/en/lo...operators.html

HTH


ND NOT a.auction =c.archivedT results in no rows

AND a.auction <> c.archived results in all rows

so niether one filters out the archived auctions
Jul 17 '05 #5

"red" <gr*****@reenie .org> wrote
$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b,
PHPAUCTIONXL_ar chived c
WHERE a.auction=b.id
AND a.auction !=c.archived";

How do I get this to work ?


I think there's a couple of ways to do this:

select a.auction, a.winner, b.id
from (PHPAUCTIONXL_w inners a join PHPAUCTIONXL_au ctions b on a.auction=b.id)
where not exists (select archived from PHPAUCTIONXL_ar chived c where
c.archived = a.auction)

or

select a.auction, a.winner, b.id, c.archived
from (PHPAUCTIONXL_w inners a join PHPAUCTIONXL_au ctions b on a.auction=b.id)
left join PHPAUCTIONXL_ar chived c on a.auction=c.arc hived
where c.archived is null
Think of it as a multi step process:

You need to do an inner join on a & b first,
then each row is checked against the archived table and rejected if there's
a match.

My sql is a bit rusty, and this page refreshed my memory about exists...

http://mysqld.active-venture.com/EXI...ubqueries.html

-- Dan
Jul 17 '05 #6
Dan Stumpus wrote:
"red" <gr*****@reenie .org> wrote

$query =
"SELECT a.auction, a.winner, b.id
FROM PHPAUCTIONXL_wi nners a, PHPAUCTIONXL_au ctions b,
PHPAUCTIONXL_ archived c
WHERE a.auction=b.id
AND a.auction !=c.archived";

How do I get this to work ?

I think there's a couple of ways to do this:

select a.auction, a.winner, b.id
from (PHPAUCTIONXL_w inners a join PHPAUCTIONXL_au ctions b on

a.auction=b.id) where not exists (select archived from PHPAUCTIONXL_ar chived c where
c.archived = a.auction) This is the result I got:
Error

SQL-query :

SELECT a.auction, a.winner, b.id
FROM (
PHPAUCTIONXL_wi nners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_ar chived c
WHERE c.archived = a.auction
)

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) WHERE
NOT EXISTS ( SELECT archived FROM PHPAUCTIONXL_ar ' at line 1


or

select a.auction, a.winner, b.id, c.archived
from (PHPAUCTIONXL_w inners a join PHPAUCTIONXL_au ctions b on a.auction=b.id) left join PHPAUCTIONXL_ar chived c on a.auction=c.arc hived
where c.archived is null

This is the result I got:
Error

SQL-query :

SELECT a.auction, a.winner, b.id, c.archived
FROM (
PHPAUCTIONXL_wi nners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archived
WHERE c.archived IS NULL
LIMIT 0 , 30

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) LEFT
JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archiv' at line 1

Think of it as a multi step process:

You need to do an inner join on a & b first,
then each row is checked against the archived table and rejected if there's a match.

My sql is a bit rusty, and this page refreshed my memory about exists...

http://mysqld.active-venture.com/EXI...ubqueries.html

-- Dan

Jul 17 '05 #7
Ed:

I just created the tables and fields on my xp box (mysql 4.1) and ran the
queries.
Results are below...(I shortened the tablenames by removing the PHPAUCTIONXL
prefix):

"Ed Simmons" <hu*@huh.com> wrote
SELECT a.auction, a.winner, b.id
FROM (
PHPAUCTIONXL_wi nners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_ar chived c
WHERE c.archived = a.auction
)

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) WHERE
NOT EXISTS ( SELECT archived FROM PHPAUCTIONXL_ar ' at line 1
== cut and paste from my query analyzer follows ==

SELECT a.auction, a.winner, b.id
FROM (
winners a
JOIN
auctions b ON a.auction = b.id
) where not exists (select archived from archived c
where c.archived=a.au ction)

....and it ran successfully, rejecting the transaction if it was in the
archived table,
and showing it if not archived.
Error SQL-query :

SELECT a.auction, a.winner, b.id, c.archived
FROM (
PHPAUCTIONXL_wi nners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archived
WHERE c.archived IS NULL
LIMIT 0 , 30

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) LEFT
JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archiv' at line 1


I don't know what's wrong, but the below query was cut/pasted from my query
analyzer, and it ran perfectly (also on mysql 4.1):

select a.auction, a.winner, b.id, c.archived
from (winners a join auctions b on
a.auction=b.id)
left join archived c on a.auction=c.arc hived
where c.archived is null

I was relieved that they worked right away (since my original post was just
off the top of my head).
Could you have created the tables or fields with different spellings?

Scratching my head,

-- Dan
Jul 17 '05 #8
Dan Stumpus wrote:
Ed:

I just created the tables and fields on my xp box (mysql 4.1) and ran the
queries.
Results are below...(I shortened the tablenames by removing the PHPAUCTIONXL
prefix):

"Ed Simmons" <hu*@huh.com> wrote

SELECT a.auction, a.winner, b.id
FROM (
PHPAUCTIONXL_ winners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_ar chived c
WHERE c.archived = a.auction
)

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) WHERE
NOT EXISTS ( SELECT archived FROM PHPAUCTIONXL_ar ' at line 1

== cut and paste from my query analyzer follows ==

SELECT a.auction, a.winner, b.id
FROM (
winners a
JOIN
auctions b ON a.auction = b.id
) where not exists (select archived from archived c
where c.archived=a.au ction)

...and it ran successfully, rejecting the transaction if it was in the
archived table,
and showing it if not archived.

Error SQL-query :

SELECT a.auction, a.winner, b.id, c.archived
FROM (
PHPAUCTIONXL_ winners a
JOIN PHPAUCTIONXL_au ctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archived
WHERE c.archived IS NULL
LIMIT 0 , 30

MySQL said:

You have an error in your SQL syntax near 'ON a.auction = b.id ) LEFT
JOIN PHPAUCTIONXL_ar chived c ON a.auction = c.archiv' at line 1

I don't know what's wrong, but the below query was cut/pasted from my query
analyzer, and it ran perfectly (also on mysql 4.1):

select a.auction, a.winner, b.id, c.archived
from (winners a join auctions b on
a.auction=b.id)
left join archived c on a.auction=c.arc hived
where c.archived is null

I was relieved that they worked right away (since my original post was just
off the top of my head).
Could you have created the tables or fields with different spellings?

Scratching my head,

-- Dan

Anyway I don't know what the problem was. I was kinda hopin someone
could understand and explain the error message so they could explain
what it was getting at.

Although I would love to be able to write more complex joins I decided
the whole approach was wrong. Instead of using an archive table with a
list of auctions on the auction table that are archived, I now move all
the values in the archived auctions to thier own table.

I realized that if I archived 2000 auctions a week, at the end of the
year I would have a table with over 100,000 auctions. Not too bad for
the archive because it won't get used as much. But the active auctions,
should be on separate table.
Jul 17 '05 #9

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

Similar topics

0
3057
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me solve my problem. I'll turn to MySQL doc after getting through this pressing project. Thanks a lot Roger! Babale -----Urspr=FCngliche...
2
1941
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1 that needs to "left" join to table1A, table1B, and table1C which is corrently done with the following: select table1.x, table1a.y, table1b.z,...
3
3340
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four inner joins, as follows : SELECT DISTINCT upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori...
1
2262
by: Beachvolleyballer | last post by:
hi there anyone had an idea to join following 2 queries to 1???? ----- QUERY 1 --------------------------------------------- SELECT TMS_CaseF_2.Name AS TCDomain_0, TMS_CaseF_3.Name AS TCDomain_1, TMS.CaseF.Name AS TCFolder_2, TMS_CaseF_1.Name AS TCFolder_3,
8
4965
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and also display their corresponding entries in arb, but if there is NO entry in arb I still want it to show up as NULL or something, so that I can get...
7
1676
by: Greg | last post by:
I'm a quantitative securities analyst working with Compustat data (company fiscal reports and pricing feeds). My coworker came across a problem that we fixed, but I'd like to understand 'why' it was happening and just don't get it yet. Here's the starting query (reduced to simple prefixes): ----INITIAL-----
3
23081
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but he doesn't know how to do that, or even if he can, and i don't have to time to learn DB2 from scratch right now. The following SQL Query is a...
12
18660
by: Phil Powell | last post by:
<cfquery name="getAll" datasource="#request.dsn#"> SELECT U.userID, U.fname, U.lname, U.phone, U.lastLoggedIn, U.choiceId, U.experience, T.label AS teamLabel, R.label AS roleLabel FROM User U LEFT JOIN UserTeamAssoc UTA ON UTA.userID = U.userID, Role R, UserRoleAssoc URA, Team T WHERE U.userID = URA.userID AND URA.roleID = R.roleID AND...
52
6296
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the...
12
13171
by: Chamnap | last post by:
Hello, everyone I have one question about the standard join and inner join, which one is faster and more reliable? Can you recommend me to use? Please, explain me... Thanks Chamnap
0
7888
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...
0
7811
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...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5689
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...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
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...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.