This topic has been archived. It cannot be replied.
-
工作学习 / 专业技术讨论 / 问个php/mysql的问题, 同一个裤, 两个table, 需要从第一个table里的id 列取一个值(5) 然后找到第2个table里id列(5) 对应的另一个列(name)里的值.......................... 如何?
-dream_home(moving2toronto);
2006-5-2
{526}
(#2942019@0)
-
use UPDATE
-canadiantire(轮胎(do ut des));
2006-5-2
(#2942090@0)
-
update will mess up the existing data in tables... i just want to have the results displayed.
-dream_home(moving2toronto);
2006-5-2
(#2942095@0)
-
Sorry, 没仔细看题。呵呵SELECT table2.name FROM table2,table1 WHERE table2.link_id = table1.id
or, if not all link_id map to id, then use
SELECT table2.name FROM table2 LEFT JOIN table1 ON table2.link_id=table1.id
-canadiantire(轮胎(do ut des));
2006-5-2
{195}
(#2942099@0)
-
我把$query2改成$query2="SELECT `table1` FROM `table2`, `table1` WHERE `$link_id`=$l"; 还是不work....
-dream_home(moving2toronto);
2006-5-2
{737}
(#2942110@0)
-
try this one, got to move my car, be back later...<?
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT table2.name FROM table1, table2 WHERE table2.link_id=table1.id AND table1.id<15";
$result=mysql_query($query);
$num=mysql_numrows($result);
for($i=0; $i<$num; $i++) {
$name=mysql_result($result, $i);
echo $name."<br/>";
}
mysql_close();
?>
-canadiantire(轮胎(do ut des));
2006-5-2
{404}
(#2942142@0)
-
thanks!!thanks!! 还是同样的结果... 我单独query tb2, 也是这样的结果, 难道tb2有问题? 对数据类型有要求?
第一个表的第一列叫id, 第2个表也一样, 第一个表的id 是有一个顺序的 比如 3, 5 ,11, 2 ..... 第二个表的id 也是乱的, 比如4, 22,23,325,234,.... 想读出第一个表的前3位(3,5,11)找到对应表2 id为3,5,11的 name值.....
但是anyway... 即便我 select * from table2 where id=5 还是同样的错误...
table2 的name Type: varchar Collation :latin1_swedish_ci
这些有关系吗?
-dream_home(moving2toronto);
2006-5-2
{465}
(#2942175@0)
-
OK, how about thisSELECT table2.name FROM table2 WHERE table2.link_id = (SELECT id FROM table1 ORDER BY id LIMIT 3)
-canadiantire(轮胎(do ut des));
2006-5-2
{97}
(#2942194@0)
-
Should we add `` around the table name? such as `table1`?
-leoxxll(leoxxll);
2006-5-2
(#2942569@0)
-
Yes you can but you don't have to unless you use keyword or special characters as field name.
-canadiantire(轮胎(do ut des));
2006-5-2
(#2942575@0)
-
umm... then another thought is, are the table names case-sensitive?
-leoxxll(leoxxll);
2006-5-2
(#2942590@0)
-
Oh, if you are considering the LZ's post, there is already a final solution (#2942527@0). I discussed with LZ it under the table, sorry about that, :-p
-canadiantire(轮胎(do ut des));
2006-5-2
(#2942639@0)
-
The final corect one ...
-canadiantire(轮胎(do ut des));
2006-5-2
{698}
(#2942527@0)
-
感謝輪胎店.... 全部問題解決... everything is running!!!
-dream_home(moving2toronto);
2006-5-2
(#2942678@0)