sql-选择一个包含2个表的列

时间:2019-02-05 15:37:24

标签: mysql sql mariadb

我有两个表,我想要一个结果:如果CID和CID2相等,则打印有关CID2值的CPU

  handleCheckboxClick = (event, id) => {
    event.stopPropagation();
    console.log("checkbox select");
    const { selected } = this.state;
    const selectedIndex = selected.indexOf(id);
    let newSelected = [];

    if (selectedIndex === -1) {
      newSelected = newSelected.concat(selected, id);
    } else if (selectedIndex === 0) {
      newSelected = newSelected.concat(selected.slice(1));
    } else if (selectedIndex === selected.length - 1) {
      newSelected = newSelected.concat(selected.slice(0, -1));
    } else if (selectedIndex > 0) {
      newSelected = newSelected.concat(
        selected.slice(0, selectedIndex),
        selected.slice(selectedIndex + 1)
      );
    }

    this.setState({ selected: newSelected });
  };
  handleRowClick = (event, id) => {
    console.log("row link");
  };
...
<TableRow
  hover
  onClick={event => this.handleRowClick(event, n.id)}
  role="checkbox"
  aria-checked={isSelected}
  tabIndex={-1}
  key={n.id}
  selected={isSelected}
>
  <TableCell className="selectCheckbox" padding="checkbox">
    <Checkbox
      onClick={event =>
        this.handleCheckboxClick(event, n.id)
      }
      className="selectCheckbox"
      checked={isSelected}
    />

结果:

first table A :
+------+---------------------+--------------------+-----------------+
| CID  | time                | step               | time_in_seconde |
+------+---------------------+--------------------+-----------------+
|    1 | 2017-07-27 06:26:50 | gege               |          10.229 |
|    2 | 2017-07-27 06:26:58 | rere               |          10.239 |
|    3 | 2017-07-27 06:35:52 | gege               |          13.229 |
|    4 | 2017-07-27 06:36:56 | titi               |          12.823 |
|    5 | 2017-07-27 06:55:04 | fefe               |          12.667 |



second table B  :
+------+---------------------+-----------------+
| CID2 | time                | cpu             |
+------+---------------------+-----------------+
|    3 | 2017-07-27 06:35:52 |          0.01   |
|    4 | 2017-07-27 06:36:56 |          0.05   |
|    5 | 2017-07-27 06:55:04 |          0.03   |

我的SQL请求不起作用:

+------+---------------------+-----------------+
| CID2 | time                | cpu             |
+------+---------------------+-----------------+
|    1 | 2017-07-27 06:26:50 |                 |
|    2 | 2017-07-27 06:26:58 |                 |
|    3 | 2017-07-27 06:35:52 |          0.01   |
|    4 | 2017-07-27 06:36:56 |          0.05   |
|    5 | 2017-07-27 06:55:04 |          0.03   |

我使用mariadb,并且我需要条件时间= 2017-07-27 06:55:04,因为我有数百万行

您有任何想法吗,谢谢您提供的任何信息

2 个答案:

答案 0 :(得分:0)

您需要使用左连接的查询,如下所示:

select t1.CID, t1.time, t2.CPU
from table1 t1
left outer join table2 t2 on t2.CID2 = t1.CID

答案 1 :(得分:0)

您可以通过左连接来实现,例如

select 
  ta.CID, ta.time,tb.cpu 
from 
  TableA ta 
left join 
  TableB tb on ta.CID=tb.CID2

如果要避免cpu为空,请将其转换为其他值,例如ISNULL(cpu,0)

为0

在这里查看w3schools上的不同联接类型:https://www.w3schools.com/sql/sql_join_left.asp