1

I am trying to output the same field twice in my SELECT but with different criteria for that field in the same row.

If I had to write it out in incorrect SQL syntax, but hopefully illustrates my point

SELECT names.FirstName WHERE xyz, names.FirstName WHERE abc FROM names; 

I want the First Name column to repeat in the same row, but to SELECT using different criteria

.

The actual example is as follows, I have two tables,

names - unique ID, first name, last name

+----+---------+-------+
| ID |  First  | Last  |
+----+---------+-------+
|  1 | Husband | Hlast |
|  2 | Wife    | Wlast |
|  3 | Friend  | Flast |
+----+---------+-------+

and another table,

groups - Unique ID , Unique ID of the head of house

+----+-------+
| ID | HOHID |
+----+-------+
|  1 |     1 |
|  2 |     1 |
|  3 |     3 |
+----+-------+

I would like the output to be the listing of each person's First and Last name as well as the First and Last name of their head of house

+----+---------+-------+----------+---------+
| ID |  First  | Last  | HOHFirst | HOHLast |
+----+---------+-------+----------+---------+
|  1 | Husband | Hlast | Husband  | Hlast   |
|  2 | Wife    | Wlast | Husband  | Hlast   |
|  3 | Friend  | Flast | Friend   | Flast   |
+----+---------+-------+----------+---------+

I can get the head of households First and Last

SELECT names.First, names.Last WHERE names.id=groups.HOHid FROM names, groups;

and separately I can get the individuals first and last

SELECT names.First, names.Last WHERE names.id=groups.id FROM names, groups;

but I can't figure out how to join the two as separate columns as the output would need to be names.First and names.Last twice but with two separate WHERE clauses. I am very new to sql and any help would be appreciated.

Jarred
  • 11
  • 2
  • Nice use of the edit functionality! You're improving your question with every edit! :) Might I suggest using the code-markdown for your select queries? – Per Enström Apr 26 '17 at 12:40
  • 1
    Bless your heart @PerEnström you've also solved my problem of getting tables to paste properly as well. If you have any other thoughts on how to make my question more clear please feel free to let me know – Jarred Apr 26 '17 at 12:50
  • In your last two samples of sql code you are missing the FROM clause which makes the whole statement invalid. What you need is just to join your table with itself `select * from yourtable y1 inner join yourtable y2 on y1.ID = y2.HOHID` – Jorge Campos Apr 26 '17 at 12:57
  • Which makes your question a duplicate of: http://stackoverflow.com/questions/14796738/inner-join-same-table – Jorge Campos Apr 26 '17 at 12:58
  • @JorgeCampos Sorry Jorge, that was a typo on my end. I want something along the lines of SELECT names.FirstName WHERE xyz, names.FirstName WHERE abc FROM names; Syntax is incorrect here but trying to convey the gist. – Jarred Apr 26 '17 at 12:59
  • Still wrong, the from clause must come before the where clause. Also I didn't understand your pseudo sql code `WHERE xyz, names.FirstName` what this means? What is `xyz` in this context? Also for the `abc` on the other where. – Jorge Campos Apr 26 '17 at 13:02
  • 1
    The example table of the result clearly shows what he wants. But yes, your linked questions seem to solve the problem on a quick glance. – Per Enström Apr 26 '17 at 13:03
  • 1
    If the question against which this problem has been closed doesn't solve the problem, see http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry Apr 26 '17 at 13:03
  • As seen in the link @Strawberry posted, http://sqlfiddle.com/ is a valuable tool to both experiment yourself and give possible answerers (?) a starting point of experimenting. – Per Enström Apr 26 '17 at 13:05
  • @JorgeCampos Your linked solution worked, thank you for the assistance. – Jarred Apr 26 '17 at 13:16
  • 1
    Possible duplicate of [INNER JOIN same table](http://stackoverflow.com/questions/14796738/inner-join-same-table) – Jorge Campos Apr 26 '17 at 13:17
  • 1
    @PerEnström Thanks for your help as well. Don't think anyone would have understood what I was asking without you. – Jarred Apr 26 '17 at 13:18
  • @Jarred Welcome to StackOverflow! – Per Enström Apr 26 '17 at 13:33

0 Answers0