views:

60

answers:

3

I'm not sure if "linearization" is the proper term, but I need a query that will output something like this:

item_name    item_price  first_name  last_name
-----------------------------------------------
'camera'    '100'        'Little'    'Timmy'
'computer'  '200'        'Little'    'Timmy'

Here's my DB: http://pastebin.com/iS4QKHEb

Any help is greatly appreciated. Thanks.

A: 

Can you explain what you're looking for? An SQL query to just display all data? Something like

SELECT *

or

SELECT item_name, item_price, first_name, last_name

?

EboMike
item_name, item_price, first_name, and last_name are not column names; they are column values. Additionally, item_name and item_price are repeating, while first_name and last_name are not.The pastebin link I provided should help explain my headache.
StackOverflowNewbie
A: 

Here's:

SELECT MAX(CASE WHEN f.name = 'item_name' THEN fd.value ELSE NULL END) AS item_name,
       MAX(CASE WHEN f.name = 'item_price' THEN fd.value ELSE NULL END) AS item_price,
       MAX(CASE WHEN f.name = 'first_name' THEN fd.value ELSE NULL END) AS first_name,
       MAX(CASE WHEN f.name = 'last_name' THEN fd.value ELSE NULL END) AS last_name
  FROM FIELDDATA fd
  JOIN FIELD f ON f.id = fd.fieldid

But the problem is I don't see what there is to group by in order to get the two rows you want as output - the datasourceid is the same for all the records, so it can't be used.

And if you [likely] want this to be dynamic, it'll require dynamic SQL via MySQL Prepared Statement syntax.

OMG Ponies
@OMG Ponies: the result of the SQL was just the first row of the results I needed. FieldData->FieldId = 100 and 101 are repeating (see Field->FieldSetId = 1 and FieldSet->Id = 1 and FieldSet->IsRepeating = 1). Does that help any? And yes, this needs to be dynamic since I don't know upfront 'item_name', 'item_price', etc. Is there a way to do this via one query (even if it is nested)?
StackOverflowNewbie
@OMG Ponies: I'm happy to build the SQL query in PHP -- as long as I have 1 final SQL to execute in the end.
StackOverflowNewbie
@StackOverflowNewbie: That's fine, but the fundamental issue is the data issue I highlighted.
OMG Ponies
A: 

Unfortunately - if you've given us everything you have... "you can't get there from here".

If you want to get...

item_name    item_price  first_name  last_name
-----------------------------------------------
'camera'    '100'        'Little'    'Timmy'
'computer'  '200'        'Little'    'Timmy'

... from ...

INSERT INTO `fielddata` (`Id`, `DataSourceId`, `FieldId`, `Value`) VALUES
(1, 15, 100, 'camera'),
(2, 15, 101, '100'),
(3, 15, 100, 'computer'),
(4, 15, 101, '200'),
(5, 15, 102, 'Little'),
(6, 15, 103, 'Timmy');


INSERT INTO `field` (`Id`, `FieldAttributeId`, `FieldSetId`, `FormId`, `LookupFieldTypeId`, `Description`, `DisplayOrder`, `IsDeleted`, `Label`, `Name`) VALUES
(102, 0, 0, 1, 6, 'First Name Description', 0, 0, 'First Name Label', 'first_name'),
(103, 0, 0, 1, 6, 'Last Name Description', 0, 0, 'Last Name Label', 'last_name'),
(100, 0, 1, 1, 6, 'Item Name Description', 0, 0, 'Item Name Label', 'item_name'),
(101, 0, 1, 1, 6, 'Item Price Description', 0, 0, 'Item Price Label', 'item_price');

You'll need to be able to pull each column as a sub-select like so...

select fd.value, 
  from fielddata fd
 inner join field f on (fd.FieldId = f.Id)
 where f.name = 'item_name'

Problem is, once you get those put together, you don't have a column to "glue" the recordsets back together - eg you won't know which price goes with which name because there's not a item_id of sorts on the fielddata table.

Let us know if there's something that accidentally got left out.

Just out of curiosity - is this class work? Looks like an exercise of sorts...

Good luck!

Bobby B
@Bobby B: no, this is not class work. It's for an actual application I am working on.Can't Field->FieldSetId act as the "glue?"
StackOverflowNewbie
Sorry - no offense intended with that class work question. FieldSetId won't accomplish what we need. We need a column in 'fielddata' that ties all attributes together to one "thing". A column that says, this name and this price both belong to the same object (e.g. camera).
Bobby B