English 中文(简体)
MySQL基于唯一ID添加数据
原标题:MySQL Add Data Based on Unique ID
  • 时间:2010-10-06 23:23:51
  •  标签:
  • mysql

我一直在四处寻找MySQL查询问题的答案,但一直未能找到答案。感谢您的帮助。。。Ps.我是个初学者。

Issue: I have two tables, one is a main table with several columns and a unique ID column (sequential numbers) which is also the key. This table has had some rows of data deleted that were not needed. So the remaining ID are for example 1,3,5,7 etc.

我还有第二个表,其中有两列,一列是唯一ID(键),另一列是文本。两个表最初都属于同一个表,但第二个表是在处理第一个表的同时提取和存储的;基本上它们具有相同的ID(密钥)。该表仍然具有所有ID 1,2,3,4,5,6,7等。

现在,我想将第二个表中的带有文本的列添加回来,并将其与第一个表中具有相同ID的行相匹配。第二个表中与第一个表没有匹配关键字的任何文本都应该简单地省略。

表插图

Table 1
ID Field 1 (Here I want the text from Table 2; only matching IDs)
1 Bla
3 Bla
5 Bla
7 Bla

Table 2
ID Text
1 Bla
2 Bla 3 Bla 4 Bla 5 Bla 6 Bla 7 Bla

如果您能帮助我撰写此查询,我将不胜感激。

Thank you, Patrik

问题回答

您可以通过简单的联接来完成此操作。将检索两个表中仅存在具有相同id的行:

SELECT t1.id, t2.text
FROM table1 t1
INNER JOIN table2 t2 ON (t2.id = t1.id)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签