English 中文(简体)
缩略语
原标题:SQL to combine 2 tables

我有2个单独的问询,这些问答表如下:

===========================
Id   f_name     l_name  
===========================
15   Little     Timmy
16   John       Doe
17   Baby       Jessica
---------------------------


===========================
Id   item_name  item_price
===========================
15   Camera     100
15   Computer   200
16   Pony       55
---------------------------

在我的SQL中,我如何把这二者结合起来:

===================================================
Id   f_name     l_name     item_name     item_price
===================================================
15   Little     Timmy      Camera        100
15   Little     Timmy      Computer      200
16   John       Doe        Pony          55
17   Baby       Jessica
---------------------------------------------------

非常感谢任何援助。 感谢。

最佳回答

页: 1 加入:

SELECT names.Id, names.f_name, names.l_name, prices.item_name, prices.item_price
FROM names
 LEFT OUTER JOIN prices
   ON names.Id = prices.Id
问题回答

select
name_table.Id, name_table.f_name, name_table.l_name,
item_table.item_name,
item_table.item_price from name_table left join item_table on name_table.Id = item_table.Id

欢乐!

有许多类型的Joins,在数据库管理中是一个非常有趣和重要的专题。 缩略语 说明使用不同种类的结合和案例。

如果你想在申请中定期作这一选择发言,你不妨考虑在数据库中形成一种看法。 一种观点基本上是一个以上表格中的数据组合。 在这里使用一种观点的一个主要好处是,你要说一个更简单的问题,这样,你就不是要加入,而只是从这个角度选择所有领域。

你的CREATE VIEW的发言将越来越多地像Doug的回答那样,而你的选择则会是这样。

选定ID, f_name, l_name, items_name, items_price from my_view;

rel=“nofollow noreferer”> 关于MySQL CREATE VIEW的更详细情况

如果你想在数据库中建立一个这样结果的表格,那么,这些选择/任务中的任何选择都属于一个固定的表格。

CREATE TABLE foobar SELECT ...




相关问题
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 ...

热门标签