简体   繁体   English

如果我知道MySQL同一行中其他列的值,如何获取列的值?

[英]How to get the value of column if I knew the value of other column in the same row in MySQL?

I have to retrieve the column data by using the other column data in the same row which I already knew. 我必须使用我已经知道的同一行中的其他列数据来检索列数据。

Eg. 例如。 Table

table Name: fixtures 桌子名称:灯具

fix_id | fix_against
1837 |  Aus Vs. Eng
2942 |  Ind Vs. SA

Here I already knew the 'fix_id'.. So, I need to retrieve the 'fix_against' value using the 'fix_id' in the same row. 在这里,我已经知道了'fix_id'。因此,我需要使用同一行中的'fix_id'来检索'fix_against'值。

Suppose, if fix_id = 1837 , I will retrieve Aus vS Eng (fix_against) value. 假设,如果fix_id = 1837 ,我将获取Aus vS Eng (fix_against)值。

Here is what I tried. 这是我尝试过的。

global $wpdb; $known_value = 1837;
  $sql = $wpdb->prepare( "SELECT fix_against FROM fixtures WHERE fix_id = '$known_value'");
$result = $wpdb->get_results( $sql );echo $result;

This doesn't apparently work. 这显然行不通。 Any suggestion will be highly appreciated. 任何建议将不胜感激。

Your query is right. 您的查询是正确的。 Your problem is here : 您的问题在这里:

$result = $wpdb->get_results( $sql );
echo $result;

By default, $wpdb->get_results returns an array of objects. 默认情况下, $wpdb->get_results返回一个对象数组。 In your case, something like $result[0]->fix_against would work. 在您的情况下,类似$result[0]->fix_against将起作用。 However, if you only need value of one cell, you're better off using $wpdb->get_var 但是,如果只需要一个单元格的值,则最好使用$wpdb->get_var

$fix_against = $wpdb->get_var($sql);
echo $fix_against;

If you only need one row, you could use $wpdb->get_row 如果只需要一行,则可以使用$wpdb->get_row

$result= $wpdb->get_row($sql);
echo $result->fix_against;

You can learn more about wpdb class here 您可以在此处了解有关wpdb类的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM