php - Complex MySQL-query, union, intersect or natural join?

604

Having an odd problem with a MySQL query, I can't really figure out how to get the data organized as I wish.

I'm building search-script in PHP, but the database structure isn't in the way that I would like.

Okay, say that I have three tables (these are completely made up):

EMPLOYES
id      name        city        hired 

Now, if I stick to LEFT JOINs etc. I am able to get a result that look like this, where it repeats every row in in the Salary table:

{-code-2}

But what I really want is something like this:

{-code-3}

Any way that I can achieve this with SQL?

225

Answer

--------- 1 Jim 0810 2001 2 Stan 6777 2002 3 George 6532 2009 SALARY id amount year
252

Answer

-- 1 2000 2009 1 2500 2010 1 2800 2011 2 2100 2009 2 2200 2010 2 2500 2011 3 2200 2009 3 2300 2010 3 2800 2011 CONTACT city areacode cityname
927

Answer

--------- 0810 0300 Tampa 6777 0800 Miami 6532 0210 Atlanta|||name cityname hired salary
950

Answer

---- Jim Tampa 2001 2000 Jim Tampa 2001 2500 Jim Tampa 2001 2800 ...|||name cityname hired 2009 2010 2011
169

Answer

-------- Jim Tampa 2001 2000 2500 2800 ...
75

Answer

Solution:

You need aPIVOT query. As your tables are made up anyway I'll work off this made up table so you can see the approach.

name    cityname    year   salary

To pivot this you would use

{-code-3}
940

Answer

---- Jim Tampa 2009 2000 Jim Tampa 2010 2500 Jim Tampa 2011 2800|||SELECT name, cityname, MAX(CASE WHEN year = 2009 then salary end) AS `2009`, MAX(CASE WHEN year = 2010 then salary end) AS `2010`, MAX(CASE WHEN year = 2011 then salary end) AS `2011`, FROM T GROUP BY name, cityname
383

Answer

Solution:

SELECT EMPLOYES.*, GROUP_CONCAT(salery) 
FROM EMPLOYES JOIN SALERY USING(id) 
GROUP BY id

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

But you better do it by hand, not in SQL. When I say "by hand" I mean by writing a program that does that for you. We are all programmers here, aren't we?

394

Answer

Solution:

As Martin commented, what youre looking for is called PIVOT in SQL Server. AFAIK MySQL doesn't support it. If you know the year range youre intrested in beforehand then you should be able to constructing the query using LEFT JOINs, something like

SELECT
  E.name,
  S2009.amount AS `2009`,
  S2010.amount AS `2010`,
  ...
FROM EMPLOYES E
LEFT JOIN SALARY S2009 ON(S2009.id = E.id)AND(S2009.year = 2009)
LEFT JOIN SALARY S2010 ON(S2010.id = E.id)AND(S2010.year = 2010)
...

People are also looking for solutions to the problem: php - Watermark rotation in codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.