MySQL选择特定列 [英] MySQL Select Specific Column

查看:93
本文介绍了MySQL选择特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,我想知道如何从一行中选择特定的列.例如,如果我想显示Joe Bugly的名字,其中ID,名字,姓氏和电子邮件都是列.

I'm new to PHP and I was wondering how to select a specific column from a row. For example if I wanted to display the first name of Joe Bugly where ID, first name, last name, and email are all columns.

推荐答案

假设您不希望使用select * from ... (a)返回的所有列,则只需列出明确显示所需的列:

Assuming that you don't want all columns that would be returned with select * from ... (a), you can simply list the desired columns explicitly:

select fname, lname from ...

例如,假设您知道用户ID为jbug01,并且只需要相应的电子邮件地址:

For example, suppose you know your user ID is jbug01 and you just want the corresponding email address:

select email
from   users
where  userid = 'jbug01'

就在PHP中执行此操作而言,以下代码段可能会有所帮助:

In terms of doing this within PHP, the following code snippet may help:

<?php
    $conn = mysql_connect ("localhost", "paxdiablo", "supersekritsauce");
    if (!$conn) {
        die ('Could not connect: ' . mysql_error());
    }
    mysql_select_db ("my_database", $conn);
    $result = mysql_query ("select email from users where userid = 'jbug01'");
    while ($row = mysql_fetch_array ($result)) {
        echo $row['email'] . "<br />";
    }
    mysql_close ($conn);
?>


(a)在少数很少情况下,选择*是有意义的(除了 来获取所有列的工具,例如作为数据库查看者).


(a) There are precious few cases where selecting * makes sense (other than tools that need to get all columns such as DB viewers).

您通常应该更明确地选择列.与仅盲目选择所有内容相比,这可以使您在处理过程中更早地发现架构更改问题.

You should usually prefer to be explicit with your column selections. This may allow you to detect problems with schema changes much earlier in the process than will be the case if you just blindly select everything.

这也将导致较少的信息传输,这对于所有数据库都在同一盒子上运行的小型数据库或系统来说可能并不重要,但它会影响系统的可扩展性,无论是在数据方面还是在数据方面网络的大小和分布.

It will also result in less information being transmitted which may not be important for small databases or systems where everything runs on the same box but it will affect scalability of your system, both in terms of data size and distribution across a network.

这篇关于MySQL选择特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆