Bluehost.com Web Hosting $6.95

Help with a query

This is a discussion on Help with a query within the MySQL Database forums, part of the Database Forums category; Hi, can anyone help me with a query? Let's say I have two tables: - books (id INT, name VARCHAR, ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-07-2006
hjsb85@gmail.com
 
Posts: n/a
Default Help with a query

Hi,

can anyone help me with a query?

Let's say I have two tables:
- books (id INT, name VARCHAR, publisher_id INT)
- publishers (id INT, name VARCHAR)

And I'd like to have a query to select and sort records in 'books' by
the publishers.name that corresponds with books.publisher_id

I'm a newbie, so excuse the newbie question.



Thanks

Reply With Quote
  #2 (permalink)  
Old 12-07-2006
hjsb85@gmail.com
 
Posts: n/a
Default Re: Help with a query

I forgot to add:
The important thing is that the fields have same names in both tables
and they can't be changed.

Reply With Quote
  #3 (permalink)  
Old 12-07-2006
Nicholas Sherlock
 
Posts: n/a
Default Re: Help with a query

hjsb85@gmail.com wrote:
> I forgot to add:
> The important thing is that the fields have same names in both tables
> and they can't be changed.


To deal with duplicate field names, you simply need to prefix the name
of the table to the name of the field: books.id, publishers.id.

Cheers,
Nicholas Sherlock

--
http://www.sherlocksoftware.org
Reply With Quote
  #4 (permalink)  
Old 12-07-2006
hjsb85@gmail.com
 
Posts: n/a
Default Re: Help with a query

I did it with
SELECT * FROM books LEFT JOIN publishers ON publisher_id =
publishers.id

only since the tables have the same column names, now the array the
record is in doesn't have associative values for the duplicate names. I
instead get like:

array(8) {
[0]=>string(1) "3"
["id"]=> string(1) "1"
[1]=> string(11) "The Big Dork (Unabridged)"
["name"]=>string(11) "Penguin Books"
[2]=>string(1) "1"
["publisher_id"]=>string(1) "1"
[3]=>string(1) "1"
[4]=>string(11) "Penguin Books"
}

How do I fix the query so I assign different column names without
changing the table itself?


Thanks

Reply With Quote
  #5 (permalink)  
Old 12-07-2006
Kees Nuyt
 
Posts: n/a
Default Re: Help with a query

On 7 Dec 2006 03:08:56 -0800, hjsb85@gmail.com wrote:

> Let's say I have two tables:
> - books (id INT, name VARCHAR, publisher_id INT)
> - publishers (id INT, name VARCHAR)
>
> And I'd like to have a query to select and sort
> records in 'books' by the publishers.name that
> corresponds with books.publisher_id
>
> I did it with
> SELECT * FROM books LEFT JOIN publishers
> ON publisher_id = publishers.id
>
> only since the tables have the same column names, now the array the
> record is in doesn't have associative values for the duplicate names. I
> instead get like:
>
> array(8) {
> [0]=>string(1) "3"
> ["id"]=> string(1) "1"
> [1]=> string(11) "The Big Dork (Unabridged)"
> ["name"]=>string(11) "Penguin Books"
> [2]=>string(1) "1"
> ["publisher_id"]=>string(1) "1"
> [3]=>string(1) "1"
> [4]=>string(11) "Penguin Books"
> }
>
> How do I fix the query so I assign different column names without
> changing the table itself?


SELECT
B.id AS book_id
,B.name AS book_name
,publisher_id
,P.name as publisher_name
FROM books AS B
LEFT JOIN publishers AS P
ON publisher_id = P.id
ORDER BY P.name;

(untested)
Note this will list all books, even when they don't have a
publisher, and it doesn't list publishers which didn't publish
any book.

>Thanks


YW
--
( Kees
)
c[_] Graduate school is where you learn to call a spade a leveraged
tactile-feedback geomass delivery system. (Martha Koester) (#190)
Reply With Quote
  #6 (permalink)  
Old 12-09-2006
hjsb85@gmail.com
 
Posts: n/a
Default Re: Help with a query

A bit complicated but I finally got it. Thanks :)

Kees Nuyt wrote:
> On 7 Dec 2006 03:08:56 -0800, hjsb85@gmail.com wrote:
>
> > Let's say I have two tables:
> > - books (id INT, name VARCHAR, publisher_id INT)
> > - publishers (id INT, name VARCHAR)
> >
> > And I'd like to have a query to select and sort
> > records in 'books' by the publishers.name that
> > corresponds with books.publisher_id
> >
> > I did it with
> > SELECT * FROM books LEFT JOIN publishers
> > ON publisher_id = publishers.id
> >
> > only since the tables have the same column names, now the array the
> > record is in doesn't have associative values for the duplicate names. I
> > instead get like:
> >
> > array(8) {
> > [0]=>string(1) "3"
> > ["id"]=> string(1) "1"
> > [1]=> string(11) "The Big Dork (Unabridged)"
> > ["name"]=>string(11) "Penguin Books"
> > [2]=>string(1) "1"
> > ["publisher_id"]=>string(1) "1"
> > [3]=>string(1) "1"
> > [4]=>string(11) "Penguin Books"
> > }
> >
> > How do I fix the query so I assign different column names without
> > changing the table itself?

>
> SELECT
> B.id AS book_id
> ,B.name AS book_name
> ,publisher_id
> ,P.name as publisher_name
> FROM books AS B
> LEFT JOIN publishers AS P
> ON publisher_id = P.id
> ORDER BY P.name;
>
> (untested)
> Note this will list all books, even when they don't have a
> publisher, and it doesn't list publishers which didn't publish
> any book.
>
> >Thanks

>
> YW
> --
> ( Kees
> )
> c[_] Graduate school is where you learn to call a spade a leveraged
> tactile-feedback geomass delivery system. (Martha Koester) (#190)


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:20 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0