Connecting Tech Pros Worldwide Forums | Help | Site Map

Join same column with different data.

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
Hello Folks,

I have an interesting problem, let me start off with, this database is not of my design, is an absolute mess, and there is nothing I can do about it.

Expand|Select|Wrap|Line Numbers
  1. SELECT distinct(`Tickets`.`EffectiveId`), `Tickets`.`Subject`, `Tickets`.`Created`, `Transactions`.`OldValue`,
  2. `Transactions`.`NewValue`, `Transactions`.`Created` as `Tcreated`,
  3. `ObjectCustomFieldValues`.`Content` as `sev`,
  4. `ObjectCustomFieldValues`.`Content` as `tt`
  5. from `Tickets`
  6. Left join (`Transactions`, `ObjectCustomFieldValues`)
  7. on (`Tickets`.`EffectiveId` = `Transactions`.`ObjectId`
  8. and `ObjectCustomFieldValues`.`ObjectId` = `Tickets`.`EffectiveId`)
  9. where `Tickets`.`id` = `Tickets`.`EffectiveId`
  10. and `Tickets`.`Created` >='2009-08-01 07:00:00'
  11. and `Tickets`.`Created` <= '2009-08-31 23:59:59'
  12. and (`Tickets`.`Status` = 'resolved'
  13. or `Tickets`.`Status` = 'closed')
  14. and `Transactions`.`Type` = 'Status'
  15. and (`ObjectCustomFieldValues`.`CustomField` = '32' or `ObjectCustomFieldValues`.`CustomField`= '24')
  16. and `ObjectCustomFieldValues`.`Disabled` = '0'
  17. order by `Tcreated`, `Tickets`.`EffectiveId` asc;
  18.  
That be the query I am using, at the moment. the only place where I am having problems is on the ObjectCustomFieldValues table. In that table there are 2 identifiers that I can use to pull data, the ObjectId and the CustomField, The CustomField is for equating a line with an actual custom field ID (elsewhere in the database), and the Object ID is a global ID through out the database. I need two different pieces of information from this table in a single call, and I am stumped.

I would like to see the sev and tt as separate columns with the data for their CustomField id in each row returned.

If I cannot figure this out, I'll just start writing to a more useful database, where the information for each field I need in in a separate column or table etc... I am just hoping I can get some help before it gets to this point.

Thanks,
TC

Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#2: 3 Weeks Ago

re: Join same column with different data.


i guess im not understanding your question. you want the sev and tt in two sepearte columns in the query?

but your query is pulling them as 2 seperate coulmns. with each its own custom field.

can you elaborate more on what your trying to do?
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: Join same column with different data.


`ObjectCustomFieldValues`.`Content` as `sev`,
`ObjectCustomFieldValues`.`Content` as `tt`


So the return value of this query looks somewhat like this:

EffectiveId, 'Subject', Created, OldValue, NewValue, Tcreated, sev, tt
33544, 'Flapping.', '2009-08-02 22:19:18', 'new', 'open', '2009-08-02 22:34:35', 'Problem', 'Problem'
33544, 'Flapping.', '2009-08-02 22:19:18', 'new', 'open', '2009-08-02 22:34:35', 'Severity 3', 'Severity 3'

What I would like to see is:

33544, 'Flapping.', '2009-08-02 22:19:18', 'new', 'open', '2009-08-02 22:34:35', 'Severity 3', Problem


As you can see they are pulled from the same column in the same table.

but that column contains different information for each custom field, so the the ones with CustomField of 24 go into tt, and CustomField 32 goes into sev. Basically so they end up in thier own columns in the query return.

Yes I know it is confusing, like I said, I didn't write the database, which is good for single lookups, but poor for reporting on multiple entries.
Member
 
Join Date: Jan 2009
Location: USA
Posts: 118
#4: 3 Weeks Ago

re: Join same column with different data.


well with the current query you have 2 parameters that need to be meet for sev and tt. you could create a sub select query to establish the desired query results for the one entity. but doing what you looking for in one query for two of the entitys two pull the data back that matches where clause. your going to have dup entries as you do now. thats why you would need to create a sub query for the desired entity.

i would say change your join type but left join is the same as inner join.

i hope this could help.

here is a link for sub selects in mysql:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Reply