2011-05-29 99 views
0

表: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') 
+0

为什么你错了什么? – 2011-05-29 06:07:02

+0

我把代码放在phpmyadmin中,它显示你的sql语法有错误 – zhuanzhou 2011-05-29 06:11:48

+1

sql看起来不错,看到@GWW的答案,我还改变了“select tid,name”为“select ti.tid,name” – 2011-05-29 06:14:14

回答

3

我的猜测是,你需要用双引号",以取代单引号'。这是因为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

+0

select tid ,名称形式taxonomy_index作为ti离开加入taxonomy_term_data作为ti.tid ttd.tid = ttd.tid其中vid = 2和nid = 2,我把这个在phpmyadmin的SQL运行框中,它显示了一个SQL错误#1064。为什么? – zhuanzhou 2011-05-29 06:19:44

+1

我刚刚告诉过你为什么''from'拼写错误'form' – GWW 2011-05-29 06:20:12

+0

对不起。我太粗心 – zhuanzhou 2011-05-29 06:24:55

1

我写了一篇关于堆栈溢出很多次这样的链接,它是在这里你可以找到一个教程,以帮助您获得最佳的地方开始使用MySQL,好运

Php : Database and Other animals

1

我想在一个字符串变量引号因此请尝试

$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) 
+0

我把这段代码放在phpmyadmin中,它显示一个sql语法错误。为什么? – zhuanzhou 2011-05-29 06:13:34

+0

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 = 2 – zhuanzhou 2011-05-29 06:13:39

+0

@zhuanzhou:检查我的答案是否有拼错的形式。 – GWW 2011-05-29 06:17:42