473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

using database contents as column names

Hi,

I have two tables I'm trying to join up, and would like to know if the
following is possible:

table1 :
SELECT * from mediatype

+-------------+---------+
| mediatypeid | type |
+-------------+---------+
| 1 | MP3 |
| 2 | ra_low |
| 3 | ra_med |
| 4 | ra_high |
+-------------+---------+

Table 2:
SELECT trackid, mediatypeid, url from media

+---------+-------------+----------------+
| trackid | mediatypeid | url |
+---------+-------------+----------------+
| 1 | 1 | mymp3.mp3 |
| 1 | 2 | myothermp3.mp3 |
| 1 | 3 | busted.mp3 |
| 1 | 4 | newmp3.mp3 |
+---------+-------------+----------------+
What I'd like to be able to get out of these two tables is a result
like this :

+---------|-----------|----------------|------------|------------+
| trackid | MP3 | ra_low | ra_med | ra_high |
+---------|-----------|----------------|------------|------------+
| 1 | mymp3.mp3 | myothermp3.mp3 | busted.mp3 | newmp3.mp3 |
+---------|-----------|----------------|------------|------------+

Is there a way to do this completly in SQL? or i could employ PHP to
help out..

Any help greatly appreciated!

Cheers,
Guy
Jul 19 '05 #1
3 1186
SR
On 3 Feb 2004 09:45:14 -0800, Guy Bowden wrote:
Hi, [...] What I'd like to be able to get out of these two tables is a result
like this :

+---------|-----------|----------------|------------|------------+
| trackid | MP3 | ra_low | ra_med | ra_high |
+---------|-----------|----------------|------------|------------+
| 1 | mymp3.mp3 | myothermp3.mp3 | busted.mp3 | newmp3.mp3 |
+---------|-----------|----------------|------------|------------+

Hi,

basically you want to obtain a pivot table. AFAIK, no SQL dialect except M$
Access's supports it *directly*, but there's something you can do, anyway:

First issue this query:

select concat(", max(if(mediatypeid=", mediatypeid, ", url, Null)) AS ",
type) from mediatype;

This will result in:

, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high

Then use php to create the actual query string:

select trackid
, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high
from media group by trackid;

et voila...

Jul 19 '05 #2
SR
On 3 Feb 2004 09:45:14 -0800, Guy Bowden wrote:
Hi, [...] What I'd like to be able to get out of these two tables is a result
like this :

+---------|-----------|----------------|------------|------------+
| trackid | MP3 | ra_low | ra_med | ra_high |
+---------|-----------|----------------|------------|------------+
| 1 | mymp3.mp3 | myothermp3.mp3 | busted.mp3 | newmp3.mp3 |
+---------|-----------|----------------|------------|------------+

Hi,

basically you want to obtain a pivot table. AFAIK, no SQL dialect except M$
Access's supports it *directly*, but there's something you can do, anyway:

First issue this query:

select concat(", max(if(mediatypeid=", mediatypeid, ", url, Null)) AS ",
type) from mediatype;

This will result in:

, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high

Then use php to create the actual query string:

select trackid
, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high
from media group by trackid;

et voila...

Jul 19 '05 #3
SR
On 3 Feb 2004 09:45:14 -0800, Guy Bowden wrote:
Hi, [...] What I'd like to be able to get out of these two tables is a result
like this :

+---------|-----------|----------------|------------|------------+
| trackid | MP3 | ra_low | ra_med | ra_high |
+---------|-----------|----------------|------------|------------+
| 1 | mymp3.mp3 | myothermp3.mp3 | busted.mp3 | newmp3.mp3 |
+---------|-----------|----------------|------------|------------+

Hi,

basically you want to obtain a pivot table. AFAIK, no SQL dialect except M$
Access's supports it *directly*, but there's something you can do, anyway:

First issue this query:

select concat(", max(if(mediatypeid=", mediatypeid, ", url, Null)) AS ",
type) from mediatype;

This will result in:

, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high

Then use php to create the actual query string:

select trackid
, max(if(mediatypeid=1, url, Null)) AS mp3
, max(if(mediatypeid=2, url, Null)) AS ra_low
, max(if(mediatypeid=3, url, Null)) AS ra_med
, max(if(mediatypeid=4, url, Null)) AS ra_high
from media group by trackid;

et voila...

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Josep | last post by:
Hi, I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find something helpful. I can get the data,...
8
by: Tom wilson | last post by:
This is driving me nuts. I'm trying to update an Excel spreadsheet using ADO.Net and Oledb in VB.Net. The connection is open, the adapter is connected and the dataset is loaded. Here's the code...
6
by: rlrcstr | last post by:
The DBA team at the office controls the naming conventions for the database structure, but their naming convention are rather tedious. So typically I create a global module that I use as a mapping...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
4
by: danthrom | last post by:
Hi, I have a database with four tables. tbl_Client client_alias (PK, text) tbl_Matters matterID (PK, autonumber) matter (text) client_alias (foreign key, tbl_Client)
2
by: David C. Barber | last post by:
This updates a previous post. We have updated our SQL Server 2000 to new hardware by reinstalling SQL Server 2K to a new, dual processor server. We performed a full SQL backup of the entire...
10
by: shsandeep | last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development database while it took 2 hours to load in a UAT database. UAT db is partitioned. Dev db is not partitioned. the...
4
by: eeb4u | last post by:
I am connecting to MS SQL 2000 from Red Hat EL4 using freetds and currently running queries to get counts on tables etc. When running SELECT queries I notice that the data returns and I have to...
0
by: David | last post by:
You're welcome. executemany is probably a good idea here. If memory becomes a problem at some point (eg: millions of lines) you'll probably want to use an on-disk database (I suggest...
5
by: dananrg | last post by:
Is there any way to retrieve column names from a cursor using the ODBC module? Or must I, in advance, create a dictionary of column position and column names for a particular table before I can...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.