开发者

How to select one column from 3 tables

I want to select one same column from 3 tables and get a rows from this one column.

Here is the code:

mysql_query("SELECT开发者_运维技巧 * from tv,movies WHERE hash='123'");

So now i want the column called hash from the tv and movies to bring result from the hash number.


I don't have the the same columns number in tv and movies.

Make sure if the hash doesn't exist in tv then go to movies.


Make it one table with category field in it.

that's the only proper way of designing a database.


SELECT fieldName1 FROM table1 WHERE hash='123'
 UNION
SELECT fieldName1 FROM table2 WHERE hash='123'
 UNION
SELECT fieldName1 FROM table3 WHERE hash='123';


You have to select equal number of columns in both tables

SELECT tvcol1 as field1,tvcol2 as field2 from tv WHERE hash='123' 
UNION 
SELECT moviescol1 as field1,moviescol2 as field2  from movies WHERE hash='123'


Reading between the lines and based on your comments to @Bryan's answer, you want all the columns from the tv table, but if it's not found select all the columns from the movies table. In that case use two different queries:

$result = mysql_query("SELECT * FROM tv WHERE hash = '123'") or die( mysql_error() );

if( mysql_num_rows( $result ) == 0 ) {
    $result = mysql_query("SELECT * FROM movies WHERE hash = '123'") or die( mysql_error() );
}

Now $result has the columns from either tv or movies table, or false if the hash wasn't found in either of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜