如何编写mysql查询?
问题描述:
表:taxonomy_index
如何编写mysql查询?
nid tid
2 1
3 1
3 4
3 5
4 6
4 1
4 3
4 7
表:taxonomy_term_data
tid vid name
1 2 java
2 2 php
3 2 c
4 1 tag1
5 1 tag2
6 1 tag3
7 1 tag4
8 1 tag5
现在我想根据nid=$nid
得到name
哪里vid=2
?我该怎么做?
以下是我的查询码。但这是错误的。
$result = mysql_query('select tid,name form taxonomy_index as ti left join taxonomy_term_data as ttd on ti.tid=ttd.tid where vid=2 and nid=$nid')
答
我的猜测是,你需要用双引号"
,以取代单引号'
。这是因为PHP不会扩展由单引号包围的变量。
$result = mysql_query("select tid,name from taxonomy_index as ti left join taxonomy_term_data as ttd on ti.tid=ttd.tid where vid=2 and nid=$nid")
编辑:
你也有from
拼错为form
答
我想在一个字符串变量引号因此请尝试
$result = mysql_query('select tid,name from taxonomy_index as ti left join taxonomy_term_data as ttd on ti.tid=ttd.tid where vid=2 and nid='.$nid)
为什么你错了什么? – 2011-05-29 06:07:02
我把代码放在phpmyadmin中,它显示你的sql语法有错误 – zhuanzhou 2011-05-29 06:11:48
sql看起来不错,看到@GWW的答案,我还改变了“select tid,name”为“select ti.tid,name” – 2011-05-29 06:14:14