In a small library/research database application I am working on, I have a page where a user can view all of the resources they have submitted.
I have three tables for different resources – Books, Journals and Conferences. The tables look like this:
mysql> desc book;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| author | varchar(255) | YES | | NULL | |
| publishedyear | char(4) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
| edition | int(3) unsigned | YES | | NULL | |
| publisher | varchar(100) | YES | | NULL | |
| place | varchar(50) | YES | | NULL | |
| image | varchar(100) | YES | | NULL | |
| isbn | varchar(20) | YES | | NULL | |
| callnumber | varchar(30) | YES | | NULL | |
| status | int(1) unsigned | YES | | NULL | |
| abstract | text | YES | | NULL | |
| toc | text | YES | | NULL | |
| problems | text | YES | | NULL | |
| futurework | text | YES | | NULL | |
| registered | datetime | YES | | NULL | |
| mid | int(10) unsigned | NO | MUL | NULL | |
| iid | int(10) unsigned | NO | MUL | NULL | |
+---------------+------------------+------+-----+---------+----------------+
18 rows in set (0.00 sec)
mysql> desc journal;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| author | varchar(255) | YES | | NULL | |
| publishedyear | char(4) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
| journaltitle | varchar(255) | YES | | NULL | |
| volume | int(3) unsigned | YES | | NULL | |
| issue | int(5) unsigned | YES | | NULL | |
| pagenumbers | varchar(15) | YES | | NULL | |
| image | varchar(100) | YES | | NULL | |
| isbn | varchar(20) | YES | | NULL | |
| callnumber | varchar(30) | YES | | NULL | |
| status | int(1) unsigned | YES | | NULL | |
| abstract | text | YES | | NULL | |
| toc | text | YES | | NULL | |
| problems | text | YES | | NULL | |
| futurework | text | YES | | NULL | |
| registered | datetime | YES | | NULL | |
| mid | int(10) unsigned | NO | MUL | NULL | |
| iid | int(10) unsigned | NO | MUL | NULL | |
+---------------+------------------+------+-----+---------+----------------+
19 rows in set (0.00 sec)
mysql> desc conference;
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| author | varchar(255) | YES | | NULL | |
| publishedyear | char(4) | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
| conferencename | varchar(255) | YES | | NULL | |
| location | varchar(100) | YES | | NULL | |
| conferencedate | varchar(15) | YES | | NULL | |
| pagenumbers | varchar(15) | YES | | NULL | |
| image | varchar(100) | YES | | NULL | |
| isbn | varchar(20) | YES | | NULL | |
| callnumber | varchar(30) | YES | | NULL | |
| status | int(1) unsigned | YES | | NULL | |
| abstract | text | YES | | NULL | |
| toc | text | YES | | NULL | |
| problems | text | YES | | NULL | |
| futurework | text | YES | | NULL | |
| registered | datetime | YES | | NULL | |
| mid | int(10) unsigned | NO | MUL | NULL | |
| iid | int(10) unsigned | NO | MUL | NULL | |
+----------------+------------------+------+-----+---------+----------------+
19 rows in set (0.00 sec)
What I am trying to do is very simple, but I can’t seem to find a simple SQL command for it. Searching on stack overflow and google for “selecting data from multiple tables” and other similar searches returned a lot of results for ridiculously complex joins, unions, etc for databases and operations that are much more complicated than what I am trying to do here.
Is there not something like:
Select * FROM book, journal, conference WHERE mid = 4'
This returns an error: “field mid is ambiguous”.
Thanks!
**EDIT – As martijn pointed out, a lot of my problem is coming from the design of my database.
Two solutions seem to be to either redesign the database and combine book, journal and conference into one table resources
or, as a very hackish workaround
To do three separate queries:
SELECT * FROM book where mid = 4
SELECT * FROM journal WHERE mid = 4
SELECT * FROM conference WHERE mid =4
and then combine those result sets using PHP.
Probably, the reason you’re having trouble because of the database design. The definitions of the three tables are (as far as I can see) identical. Maybe you should make it one table with an
ENUMfield specifying if the entry is a book, journal or conference.EDIT: I’ve just seen this is not an easy option: you’ll still need separate tables for the info that is specific to books, journals and conferences.
If you do not want to do this, you need to specify you question a little more. Do you want to have the rows from the three tables which have
midequal to 4? In this case, you have to useUNION. If you want one line giving you the book, journal, and conference withmid = 4, use aJOIN, as thomasrutter pointed out already.EDIT: now that you point out exactly what you want the query to return, it is probably best to use ‘mu is too short”s solution. Adding the field is an elegant solution (+1 for that).
Still, I think you should consider using the
ENUMoption. I do not know what functionality would break, but fixing queries would boil down to adding aWHERE kind = 'journal'if you’re selecting fromjounalsoriginally.