mysql连接表限制特定表

时间:2015-07-14 11:09:26

标签: php mysql sql join

我已经创建了这个SQL查询,以便连接关键变量并从数据库中提取信息。

首先我使用了这个:

SELECT * 
FROM `users` a,`field_data_field_ypefthinos` b, `field_data_field_branch_chain` c, `field_data_field_taxonomy_etairia` d, `node_revision` e
WHERE a.uid = b.field_ypefthinos_uid
AND a.uid = 1
AND b.entity_id = c.entity_id
AND d.field_taxonomy_etairia_tid = c.field_branch_chain_tid
AND d.bundle = 'kouponia'
AND d.entity_id = e.nid

然后我将其转换为此,以便能够使用stmt bind_result函数

SELECT
users.uid, 
field_data_field_ypefthinos.field_ypefthinos_uid, 
field_data_field_ypefthinos.entity_id,
field_data_field_branch_chain.entity_id,
field_data_field_taxonomy_etairia.field_taxonomy_etairia_tid,
field_data_field_taxonomy_etairia.bundle,
field_data_field_taxonomy_etairia.entity_id,
node_revision.nid
FROM `users`,`field_data_field_ypefthinos`, `field_data_field_branch_chain`, `field_data_field_taxonomy_etairia`, `node_revision`
WHERE users.uid = field_data_field_ypefthinos.field_ypefthinos_uid
AND users.uid = 1
AND field_data_field_ypefthinos.entity_id = field_data_field_branch_chain.entity_id
AND field_data_field_taxonomy_etairia.field_taxonomy_etairia_tid = field_data_field_branch_chain.field_branch_chain_tid
AND field_data_field_taxonomy_etairia.bundle = 'kouponia'
AND field_data_field_taxonomy_etairia.entity_id = node_revision.nid

我接下来要做的是:

1)使用表/字段别名,以便像在第一个代码中一样更容易地维护代码。

2)重要提示:能够将表field_data_field_ypefthinos的结果限制为LIMIT 1。

我的目标是拥有这样的代码:

SELECT a.uid, b.field_ypefthinos_uid, b.entity_id, c.entity_id, d.field_taxonomy_etairia_tid, c.field_branch_chain_tid, d.bundle, d.entity_id
FROM `users` a,`field_data_field_ypefthinos` b LIMIT 1, `field_data_field_branch_chain` c, `field_data_field_taxonomy_etairia` d, `node_revision` e
WHERE a.uid = b.field_ypefthinos_uid
AND a.uid = 1
AND b.entity_id = c.entity_id
AND d.field_taxonomy_etairia_tid = c.field_branch_chain_tid
AND d.bundle = 'kouponia'
AND d.entity_id = e.nid

1 个答案:

答案 0 :(得分:0)

DISTINCT完成了这项工作,解决方案:

SELECT DISTINCT e.title
FROM `users` a,`field_data_field_ypefthinos` b, `field_data_field_branch_chain` c, `field_data_field_taxonomy_etairia` d, `node_revision` e
WHERE a.uid = b.field_ypefthinos_uid
AND a.uid = 1
AND b.entity_id = c.entity_id
AND d.field_taxonomy_etairia_tid = c.field_branch_chain_tid
AND d.bundle = 'kouponia'
AND d.entity_id = e.nid