Mysql加入覆盖提交ID

时间:2014-01-12 02:09:13

标签: mysql sql node-mysql

我有2张桌子。 submissionsusers。它们与submissions.user_id = users.id相关联。

users

+----------------+------------------------+------+-----+-------------------+-----------------------------+
| Field          | Type                   | Null | Key | Default           | Extra                       |
+----------------+------------------------+------+-----+-------------------+-----------------------------+
| id             | int(10) unsigned       | NO   | PRI | NULL              | auto_increment              |
| email          | varchar(128)           | NO   | MUL | NULL              |                             |
| hash           | varchar(64)            | NO   |     | NULL              |                             |
| salt           | varchar(32)            | NO   |     | NULL              |                             |
| username       | varchar(23)            | NO   |     | NULL              |                             |
| type           | enum('normal','admin') | NO   |     | normal            |                             |
| about          | varchar(255)           | NO   |     | NULL              |                             |
| created        | datetime               | NO   |     | NULL              |                             |
| last_login     | timestamp              | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| created_ip     | int(10) unsigned       | NO   |     | NULL              |                             |
| last_login_ip  | int(10) unsigned       | NO   |     | NULL              |                             |
| remember_me    | tinyint(3) unsigned    | NO   |     | 0                 |                             |
| avatar         | varchar(32)            | NO   |     | NULL              |                             |
| confirmed      | tinyint(1) unsigned    | NO   |     | 0                 |                             |
| confirm_code   | varchar(64)            | NO   |     | NULL              |                             |
| public_profile | tinyint(1)             | NO   |     | 1                 |                             |
+----------------+------------------------+------+-----+-------------------+-----------------------------+

submissions

+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
| Field       | Type                                                                                                                                                                                                                                                                     | Null | Key | Default | Extra          |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
| id          | int(10) unsigned                                                                                                                                                                                                                                                         | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255)                                                                                                                                                                                                                                                             | NO   |     | NULL    |                |
| slug        | varchar(255)                                                                                                                                                                                                                                                             | NO   |     | NULL    |                |
| description | mediumtext                                                                                                                                                                                                                                                               | NO   |     | NULL    |                |
| user_id     | int(11)                                                                                                                                                                                                                                                                  | NO   | MUL | NULL    |                |
| created     | datetime                                                                                                                                                                                                                                                                 | NO   |     | NULL    |                |
| category    | enum('animals-pets','autos-vehicles','careers-work','clothing','computers','cooking-food','deals','education','electronics','entertainment','general-advice','finance','health-fitness','hygiene','home-garden','how-to','misc','self-improvement','sports','traveling') | NO   |     | NULL    |                |
| type        | enum('tip','request')                                                                                                                                                                                                                                                    | NO   |     | NULL    |                |
| thumbnail   | varchar(64)                                                                                                                                                                                                                                                              | NO   |     | NULL    |                |
| removed     | tinyint(1) unsigned                                                                                                                                                                                                                                                      | NO   |     | 0       |                |
| down_votes  | int(10) unsigned                                                                                                                                                                                                                                                         | NO   |     | 0       |                |
| up_votes    | int(10) unsigned                                                                                                                                                                                                                                                         | NO   |     | 0       |                |
| score       | int(11)                                                                                                                                                                                                                                                                  | NO   | MUL | 0       |                |
| keywords    | varchar(255)                                                                                                                                                                                                                                                             | NO   |     | NULL    |                |
| ip          | int(10) unsigned                                                                                                                                                                                                                                                         | NO   |     | NULL    |                |
+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+

我试图加入这两个表来查看提交,但结果我得到以下查询给了我:

SELECT s.*, u.id, u.username FROM submissions s JOIN users u ON s.user_id = u.id WHERE s.id = 12'

永远都会给我:

   { id: 15,
     title: 'what the hell!',
     slug: 'what-the-hell',
     description: 'seriously what the hell node mysql?!',
     user_id: 15,
     created: Sat Jan 11 2014 11:58:06 GMT-0800 (PST),
     category: 'misc',
     thumbnail: '',
     removed: 0,
     down_votes: 0,
     up_votes: 0,
     score: 0,
     keywords: '!',
     ip: 127001,
     username: 'blahbster'}

提取时始终会生成submission.id,这是user.id.为什么要覆盖?

1 个答案:

答案 0 :(得分:1)

它是“覆盖”,因为两列具有相同的名称id,并且无法区分它们。

只需使用:

SELECT s.*, u.username
FROM submissions s JOIN
     users u
     ON s.user_id = u.id
WHERE s.id = 12;

并使用user_id作为用户ID,使用id作为订阅ID。