FAQ
At using a SELECT into a field referenced, I get the value from its primary
key. But how to do it for to get the value of another field?

CREATE TABLE emailType (
id bigint PRIMARY KEY,
name text
);

CREATE TABLE email (
id bigint PRIMARY KEY,
address text UNIQUE,
type bigint REFERENCES emailType(id),
);

INSERT INTO emailType (id, name)
VALUES(0, 'primary');
INSERT INTO emailType (id, name)
VALUES(1, 'secondary');
COMMIT;

INSERT INTO email (id, address, type)
VALUES(10, 'john@foo.net', 0);
INSERT INTO email (id, address, type)
VALUES(11, 'john@bar.net', 1);

* * *

query := "SELECT type FROM email WHERE address = $1"
email := "john@foo.net"
t := ""

err := db.QueryRow(query, email).Scan(&t)

t gets 0, but would be possible, without to use another query, to get the
value of field "name"?

--

Search Discussions

  • Derek Tracy at Nov 29, 2012 at 1:29 pm
    You would do an inner join on the two tables

    query := "SELECT email.type, emailType.name from email INNER JOIN emailType
    on email.type=emailType.id WHERE email.address = $1"
    email := "john@foo.net"
    t, n := "", ""
    err := db.QueryRow(query, email).Scan(&t, &n)

    That should give you what you need
    On Thu, Nov 29, 2012 at 8:13 AM, Archos wrote:

    get the value of field
    --
  • Archos at Nov 29, 2012 at 1:54 pm
    Thanks!

    El jueves, 29 de noviembre de 2012 13:29:07 UTC, Derek Tracy escribió:

    You would do an inner join on the two tables

    query := "SELECT email.type, emailType.name from email INNER JOIN
    emailType on email.type=emailType.id WHERE email.address = $1"
    email := "jo...@foo.net <javascript:>"
    t, n := "", ""
    err := db.QueryRow(query, email).Scan(&t, &n)

    That should give you what you need

    On Thu, Nov 29, 2012 at 8:13 AM, Archos <raul...@sent.com <javascript:>>wrote:
    get the value of field
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 29, '12 at 1:13p
activeNov 29, '12 at 1:54p
posts3
users2
websitegolang.org

2 users in discussion

Archos: 2 posts Derek Tracy: 1 post

People

Translate

site design / logo © 2022 Grokbase