473,388 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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_winners a, PHPAUCTIONXL_auctions 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_archived 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_winners a, PHPAUCTIONXL_auctions b,
PHPAUCTIONXL_archived c
WHERE a.auction=b.id
AND a.auction !=c.archived";

How do I get this to work ?

red
Jul 17 '05 #1
8 2042
I noticed that Message-ID: <a4**********************@twister.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**********************@twister.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_winners a, PHPAUCTIONXL_auctions 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_archived 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_winners a, PHPAUCTIONXL_auctions b,
PHPAUCTIONXL_archived 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**********************@twister.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_winners a, PHPAUCTIONXL_auctions 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_winners a join PHPAUCTIONXL_auctions b on a.auction=b.id)
where not exists (select archived from PHPAUCTIONXL_archived c where
c.archived = a.auction)

or

select a.auction, a.winner, b.id, c.archived
from (PHPAUCTIONXL_winners a join PHPAUCTIONXL_auctions b on a.auction=b.id)
left join PHPAUCTIONXL_archived c on a.auction=c.archived
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_winners a, PHPAUCTIONXL_auctions 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_winners a join PHPAUCTIONXL_auctions b on

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

SQL-query :

SELECT a.auction, a.winner, b.id
FROM (
PHPAUCTIONXL_winners a
JOIN PHPAUCTIONXL_auctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_archived 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_winners a join PHPAUCTIONXL_auctions b on a.auction=b.id) left join PHPAUCTIONXL_archived c on a.auction=c.archived
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_winners a
JOIN PHPAUCTIONXL_auctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_archived 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_archived 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_winners a
JOIN PHPAUCTIONXL_auctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_archived 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.auction)

....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_auctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_archived 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_archived 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.archived
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_auctions b ON a.auction = b.id
)
WHERE NOT
EXISTS (

SELECT archived
FROM PHPAUCTIONXL_archived 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.auction)

...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_auctions b ON a.auction = b.id
)
LEFT JOIN PHPAUCTIONXL_archived 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_archived 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.archived
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
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...
2
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...
3
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...
1
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...
8
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...
7
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...
3
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...
12
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...
52
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...
12
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.