php - 如何编写mysql查询?

标签 php mysql

表:taxonomy_index

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 where 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

关于php - 如何编写mysql查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6166225/

相关文章:

php - mod_rewrite 接管页面上的所有链接

php - 对 php 数组对象中的结果进行排序

mysql - 即使该列存在,也会出现 "Unknown Column"错误

mysql - 为什么即使有结果,count(*) 也返回 0?

php - 如果MySQL表中有多条相同的记录,如何只选择一条记录

php - 用于提取 css 规则和双大括号的正则表达式

php array shuffle/randomize array 所以没有两个相等的值不接近

php - 在选择期间从 MYSQL 中删除空值

mysql报告每日查询按类型过滤,包括丢失的分组行

mysql - 从大表中查找相似值的最佳方法