Jump to content

if statement with subqueries


emehrkay

Recommended Posts

I have an object table that I use to create unique values across tables. So the person table has a person_id field that is unique to the table and an object_id field that is unique to the whole database (I thought that this would be an elegant solution for my commenting/rating system).

 

In the object table I only store the object_id and object_type, in this case "person." My question is if I have the object_id, what would be the best way for me to run a query against the right table based on the object_type?

 

So if object_id 1 has object_type person, how would I run the subquery - SELECT * FROM person WHERE object_id = 1 ?

 

Thanks

Link to comment
Share on other sites

Object table

DROP TABLE IF EXISTS `8trk`.`object`;
CREATE TABLE  `8trk`.`object` (
  `object_id` int(15) NOT NULL auto_increment,
  `object_type` varchar(50) collate latin1_general_ci NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  PRIMARY KEY  (`object_id`)
) ENGINE=MyISAM AUTO_INCREMENT=81 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

 

Other tables

DROP TABLE IF EXISTS `8trk`.`users`;
CREATE TABLE  `8trk`.`users` (
  `user_id` int(15) NOT NULL auto_increment,
  `object_id` int(15) NOT NULL,
  `user_name` varchar(50) collate latin1_general_ci NOT NULL,
  `user_pass` text collate latin1_general_ci NOT NULL,
  `user_email` varchar(100) collate latin1_general_ci NOT NULL,
  `user_role` int(10) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

 

The object_id is a secondary key in almost every other table, I just want to run a query on the object table with that id and return the correct result set

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.