[OT} Any SQL Experts Here?

Alexander Short Alexander.Short-V7Ve2fXh0sTQT0dZR+AlfA at public.gmane.org
Thu May 28 14:06:50 UTC 2009


One way and there is perhaps better, is to use a sub-select.  This is
one way of doing it at least in T-SQL using MS SQL (what I'm more
familiar with)

SELECT	a.*,COUNT(b.*),(SELECT TOP 1 b2.FIELD FROM TABLEB b2 WHERE
b2.KEY1 = a.KEY1 AND b2.KEY2 = a.KEY2)
FROM	TABLEA a
JOIN	TABLEB b
ON	a.KEY1 = b.KEY1
AND	a.KEY2 = b.KEY2
GROUP BY a.KEY1,a.KEY2

-----Original Message-----
From: owner-tlug-lxSQFCZeNF4 at public.gmane.org [mailto:owner-tlug-lxSQFCZeNF4 at public.gmane.org] On Behalf Of Stephen
Sent: Thursday, May 28, 2009 9:32 AM
To: TO Linux User Group
Subject: [TLUG]: [OT} Any SQL Experts Here?

Any SQL experts here?

I am trying to enhance my web site and I can't come up with the query 
that I need.

Briefly, I have table A and table B

The unique primary keys in table A, are present in a column in table B. 
It is a one to many relationship.

Currently, I select all of table A, plus the row count of table B, 
indicating how many table B rows have the primary key of the table A
row.

Now, I want to add an additional field from column B, for the first 
matching row. But I need to something equivalent to a LIMIT 1 on the 
table B rows, so the final result set has the same number of entries as 
rows in table A.

I am using the PDO class in PHP against a MySQL database.

I will send the full detail to anyone willing to take a look.

Thanks!

Stephen

--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list