mysql table name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

mysql table name

Post by yshaf13 »

i'm planning on joining a bunch of tables and using a loop to display the html for every row, but in every row it has to say which table it came from. how do i get the name of the table?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're using a UNION, add it to each of the queries.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

i wasnt planniing on using union just plain old join (select col1, col2 from table1, table2...) since i'm selecting the same colums from each table i don't see how union would help. and even if i did use union how would i get the table name?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you write it into the query as a "fake" column returned by supplying a string literal. If the tables have the same structure, joining them will only return massive duplicate records or no records, depending on how you write the query. It sounds like a UNION would be best, unless you can provide more information.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

i have 5-6 tables that each have a number of colums that are the same and a few that are distinct to that table. on the page i'm talking about i want to output all the colums that are the same in each table.because of the way it's set up there shouldn't be any duplicates. are you suggesting adding a colum to each table with the table name? is that the only way?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not adding a column to the tables, but instead adding a string literal to the query as I've already said now.
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

you write it into the query as a "fake" column returned by supplying a string literal.
i'm sorry but i'm kinda new to php/mysql and i have no idea what that means.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

i'm sorry but i'm kinda new to php/mysql and i have no idea what that means.

Code: Select all

select
    'table1', *
from table1
union
select
    'table2', *
from table2
........
yshaf13
Forum Commoner
Posts: 72
Joined: Mon Apr 03, 2006 7:59 pm

re

Post by yshaf13 »

thanks for the help, i tried it but using "select 'table1', * from..." didnt work i had to say "select 'table1', col1, col2..." is there any way i could use the "*"?
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

If you are using tables with fields of the same names you will have problems using '*'. You will need to explicitly state 'table.field' in the select statement. You will have to do the same in the WHERE clause of the statement as well.

Code: Select all

SELECT tbl1.field1,tbl2.field2 FROM tbl1,tbl2 WHERE tbl1.field1 = tbl2.field2
Post Reply