How to add extra row with insert into table select from table in mysql?

时间:2019-02-09 09:35:38

标签: mysql

I have two tables: table_1 contains a post and table_2 contains all users mapped to post. now I would like to insert post into table1 and I will get a post_id, then I will insert all users who are mapped to post with user_id and post_id.

 insert into `post_users_map` (`post_id`,`user_id`,`is_owner`)
 select '12345', `user_id`, '0' from `users`
 where username in ("A","B","C")

Now I would like to insert ("12345",'123',1) as another row along with select results.

Any suggestion is appreciated and Thanks in Advance.

2 个答案:

答案 0 :(得分:2)

只需使用联合添加选择

insert into `post_users_map` (`post_id`,`user_id`,`is_owner`) 
select '12345', `user_id`, '0' 
from `users` where username in ("A","B","C")
UNION 
select '12345', '123, '1' 

答案 1 :(得分:1)

仅在选择插入后在值之后添加另一个插入。

Example Class

此外,import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import { Button,OverlayTrigger, Tooltip,Popover} from 'react-bootstrap'; //var Modal = require('react-bootstrap-modal') //require('react-bootstrap-modal') var Modal = require('react-bootstrap').Modal; //var Button = require('react-bootstrap').Button; class Example extends React.Component { constructor(props, context) { super(props, context); this.handleShow = this.handleShow.bind(this); this.handleClose = this.handleClose.bind(this); this.state = { show: false, //popup_index : this.props.popup_index }; } handleClose() { //alert('sdsd'); this.setState({ show : false }); //this.props.handleShow(this.state.show); } handleShow() { this.setState({ show: this.props.show }); } render() { const popover = ( <Popover id="modal-popover" title="popover"> very popover. such engagement </Popover> ); const tooltip = <Tooltip id="modal-tooltip">wow.</Tooltip>; return ( <div> <Modal show={this.props.handleShow} onHide={this.handleClose} > <Modal.Header closeButton> <Modal.Title>Modal heading {this.props.popup_id} </Modal.Title> </Modal.Header> <Modal.Body> <h4>Text in a modal</h4> <p> Duis mollis, est non commodo luctus, nisi erat porttitor ligula. </p> <h4>Popover in a modal</h4> <p> there is a{' '} <OverlayTrigger overlay={popover}> <a href="#popover">popover</a> </OverlayTrigger>{' '} here </p> <h4>Tooltips in a modal</h4> <p> there is a{' '} <OverlayTrigger overlay={tooltip}> <a href="#tooltip">tooltip</a> </OverlayTrigger>{' '} here </p> <hr /> <h4>Overflowing text to show scroll behavior</h4> <p> Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. </p> </Modal.Body> <Modal.Footer> <Button onClick={this.handleClose}>Close</Button> </Modal.Footer> </Modal> </div> ); } } class PalladiumHub extends React.Component { render() { return (<tr> <td>{this.props.keyuser}</td> <td>{this.props.name.name}</td> <td><button type="button" onClick={(e) => { this.props.editTask(this.props.index) }} >Edit</button><button onClick={(e) => { this.props.deleteTask(this.props.index) }}>Delete</button></td> </tr> ) } } class CallCRUD extends React.Component { constructor(props) { super(props); this.deleteTask = this.deleteTask.bind(this); this.editTask = this.editTask.bind(this); this.state = { error: null, isLoaded: false, items: [], isOpen: false, show : false, popup_index:'' }; } componentDidMount() { fetch("https://jsonplaceholder.typicode.com/users") .then(res => res.json()) .then( (result) => { this.setState({ isLoaded: true, items: result }); }, (error) => { this.setState({ isLoaded: true, error }); } ) } toggleModal(index) { this.setState({show:true,popup_index:index}); } deleteTask(index) { //alert(index); //console.log(index); //return false; let tasks = this.state.items; tasks.splice(index, 1); this.setState({ items: tasks }) } editTask(index) { this.toggleModal(index); //console.log(index); } render() { console.log(this.state.items); return ( <section> <table border="1"> <tr><th>ID</th><th>Name</th><th>Action</th></tr> { this.state.items.map((data, index) => { //return console.log(data.id); return <PalladiumHub name={data} keyuser={data.id} index={index} key={index} deleteTask={this.deleteTask} editTask={this.editTask} /> }) } </table> <Example handleShow = {this.state.show} popup_id = {this.state.popup_index} /> </section> ); } } ReactDOM.render( <CallCRUD />, document.getElementById('root') ); 允许在同一insert into `post_users_map` (`post_id`,`user_id`,`is_owner`) select 12345, user_id, 0 from `users` where username in ('A','B','C'); insert into `post_users_map` (`post_id`,`user_id`,`is_owner`) values (12345, 123, 1); 中包含更多元组。

values

或者您可以使用UNION ALL插入2个select中具有不同is_owner的user_id。

insert