tags:

views:

696

answers:

3

Yesterday I screwed up by asking question which had dupe column name by accident. This caused interesting solutions. So I will ask same question here again, but this time I will directly mysql dump, so there are no confusion.

CREATE TABLE `tbl1` (
  `UserID` char(15) DEFAULT NULL,
  `col1` char(15) DEFAULT NULL,
  `col2` char(15) DEFAULT NULL,
  `col3` char(15) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `tbl2` (
  `UserID` char(15) DEFAULT NULL,
  `col4` char(15) DEFAULT NULL,
  `col5` char(15) DEFAULT NULL,
  `col6` char(15) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO tbl1
  (`UserID`, `col1`, `col2`, `col3`)
VALUES
  ('user1', 'one', 'two', 'three');

INSERT INTO tbl1
  (`UserID`, `col1`, `col2`, `col3`)
VALUES
  ('user1', 'oneone', 'twotwo', 'threethree');

INSERT INTO tbl2
  (`UserID`, `col4`, `col5`, `col6`)
VALUES
  ('user1', 'col4name', 'col5name', 'col6name');

End result what I am looking for would look like this, after mysql query.

CREATE TABLE `endresult` (
  `UserID` char(15) DEFAULT NULL,
  `col4name` char(15) DEFAULT NULL,
  `col5name` char(15) DEFAULT NULL,
  `col6name` char(15) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

You will see that user1 has on tbl2 under col4 record "col4name" which was added by user1 for his/hers custom name for that particular column. Now I would like to see these custom column names displayed like they are displayed on "endresult" table.

I know this would be preferred to do at the end using php to display, but I really need to do this in mysql side. Thanks again

+1  A: 

I think executing the SQL statement as a string (dynamically) may be your best bet. Please be careful with respect to SQL injection and the data to expect to use within this SQL statement.

Something like:

SET @qry = (
            SELECT CONCAT('SELECT t1.userid, 
                           t1.col1 AS "', t2.col4, '", 
                           t1.col2 AS "', t2.col5, '",
                           t1.col3 AS "', t2.col6, '"
                    FROM tbl1 t1') AS QUERY_TEXT                      
            FROM tbl2 t2 
            WHERE t2.userid = @userId
           );
PREPARE stmt FROM @qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

This query will return data from tbl1, but col1, col2, and col3 are aliased by the values within the matching tbl2 record given a specific userid @userId.

Therein lies the issue, I can't seem to figure out what to expand on this issue to make it apply to multiple users in a single SELECT statement. This makes sense, because it's not possible to line up the columns correctly when they're named different (plus, I believe that this is impossible to do in SQL).

David Andres
I think this should work, but on my end, I get "Unknown column 'col4' in 'field list' ??? any idea why?
Ossi
Made a small edit to the query at the point "WHERE t2.userid = @userId" This used to be "WHERE t1.userid = @userId" t1 wasn't a part of the query. Try running the select statement (the whole SELECT CONCAT business) on its own.
David Andres
It would only need to be for single user and his her records... I was hoping to add something like WHERE user = 'user1' at the end of this query, if I get it to work
Ossi
@Hank: I keep fine tuning it. Got rid of quite a bit of unnecessary SQL. Does the current query work?
David Andres
I tried 3 of your versions and when I copy and paste from this page, it adds spaces... I am using editor to remove junk. but keep getting syntax errors. On your first and second try I had tried just what was inside CONCAT and that was running fine but my column names were like + col4 + I will try again.
Ossi
Ok looked this page and sourcecode and copied from there, got it ok, syntax looks ok with color code, (I'm using navicat) and tried running the query, it still gave me syntax error... something about NULL....
Ossi
@Hank: I've updated the query to use CONCAT. I'm not familiar with MySQL, so I wasn't aware that this is how string concatenation gets done.
David Andres
Stll no luck.... thanks for your efforts. I am heading to bed, have to get up early. will work with this again tomorrow.
Ossi
+1  A: 

I think that you would like the column name to be dynamically defined based on the contents of tbl1 and tbl2.

This is not possible, the closest you can get to this, is to dynamically craft a SQL statement with distinct alliases for the columns (based on a previous SQL query to find the proper values). This of course would be limited to the names for ONE given user since, several users may have conflicting names, but the query will take only one allias per column... (That is BTW one, though not the main reason, why this can't be done directly in SQL).

EDIT NOTE: David Andres' reply shows one way of doing such a two step query.

mjv
You've hit the nail on the head re: limited to one user at a time.
David Andres
A: 

This is from other board.... Not sure how to apply for my situation ?

Maybe something like this (done for mysql database, you should modify this for your joins):

mysql> select user, CASE WHEN user='root' THEN host WHEN user='drupal' THEN password END FROM user WHERE user<>''; +------------------+----------------------------------------------------------------------+ | user | CASE WHEN user='root' THEN host WHEN user='drupal' THEN password END | +------------------+----------------------------------------------------------------------+ | root | localhost | | root | laptop | | root | 127.0.0.1 | | debian-sys-maint | NULL | | drupal | *7AFEAE5774E672996251E09B946CB3953FC67656 | | drupal | | +------------------+----------------------------------------------------------------------+

You should make something like:

SELECT .... ON tbl1 JOIN tbl2 ON tbl1.col1=tbl2.col2 ...

Ossi