如何在mysql中将id作为键选择return

时间:2019-04-16 12:28:33

标签: mysql

我需要返回mysql查询

来自

class MyComponent extends React.Component {
  ref = React.createRef();

  componentDidMount() {
    this.onChangeMade();
  }

  onChangeMade = () => {
    const { current } = this.ref;
    const scrollHeightPadded = current.scrollHeight;

    if (scrollHeightPadded + "px" !== current.style.height) {
      current.style.height = 0;
      const height = Math.max(scrollHeightPadded, 31) + 3;
      current.style.height = `${height}px`;
    }
  };

  render() {
    return (
      <div className={`resizable-textbox ${className}`}>
        <textarea
          ref={this.ref}
          value={value}
          onChange={onChangeMade}
          onBlur={onBlur}
          readOnly={readOnly}
        />
      </div>
    );
  }
}

但是我需要在mysql中这样返回(我将它写在数组中,因为它很容易写)

SELECT name as id FROM table 

3 个答案:

答案 0 :(得分:1)

使用WHERE条件

SELECT * FROM table WHERE ID = yourId

只需将yourId替换为所需的ID号。

答案 1 :(得分:0)

您想要返回ID和名称,因此您应该将查询重写为:

SELECT id,name FROM table 

您应使用逗号分隔字段名称。

有关更多信息,您可以阅读here

答案 2 :(得分:0)

首先,使用

选择所有ID和名称
SELECT id, name FROM table SOME CONDITIONS

此查询将返回类似的结果

$result = [
  [ id => 1, name => 'name' ],
  [ id => 1, name => 'name' ],
  [ id => 1, name => 'name' ],
  [ id => 1, name => 'name' ],
]

然后排列数组

$accepted_output = array_column($result, 'id', 'name')

$accepted_output  = [
  [ 1 =>  'name' ],
  [ 1 =>  'name' ],
  [ 1 =>  'name' ],
  [ 1 =>  'name' ],
]

array_column文档-https://www.php.net/manual/en/function.array-column.php