inner-join

How to convert multiple rows to one row in SQL Server? [duplicate]

醉酒当歌 提交于 2019-12-12 06:05:30
问题 This question already has answers here : Simulating group_concat MySQL function in Microsoft SQL Server 2005? (10 answers) Closed 4 years ago . I have a problem with a query. Let's say I have two tables named PersonInfo and PersonEducation . I applied join operation on these tables with StudentId and I have a result like that. StudentIdId Name University Department Status --------------------------------------------------------------- 1 John Cambridge Computer Graduated 1 John Berkeley

MySQL Query INNER JOIN with aliases

核能气质少年 提交于 2019-12-12 05:38:49
问题 I have two tables: users and users_info users looks like this: +----+----------+-------+ | id | slug | name | +----+----------+-------+ | 1 | theploki | Kris | +----+----------+-------+ and users_info looks like this: +----+--------+----------+---------------+ | id | parent | info_key | info_val | +----+--------+----------+---------------+ | 1 | 1 | email | kris@kris.com | +----+--------+----------+---------------+ | 2 | 1 | age | 28 | +----+--------+----------+---------------+ I want to

MySQL Query - Sum the Capacity of Multiple Warehouses

妖精的绣舞 提交于 2019-12-12 05:02:45
问题 We have a MySQL database that has two tables, Warehouse and Crate Warehouse: WarehouseID (primary key) Location (varchar) Crate: CrateID (primary key) Warehouse (foreign key to a Warehouse record) Max_Capacity (int) (the amount of boxes that can be packed into a crate) I would like to write a query that returns a sum of all the crates' max_capacity for each Warehouse. I'm having trouble with duplicate entries and inner joins, so what I'm hoping for is something like: LOCATION: WAREHOUSE_ID:

Inner Join twice in the same table

烈酒焚心 提交于 2019-12-12 04:55:18
问题 im having an issue with my inner join,these are my tables with FK and PK TABLE CITY city_id (PK) city_name state TABLE DEPOT dep_id (PK) capacity city_id (FK) references CiTy TABLE MANUFACTURER manu_id (PK) manu_name city_id (FK) references city so i just want to make the result look like this: DEPOT_CITY_name(referencese from city_id), MANUFACTURER_CITY_name(references from city_id) thanks 回答1: List the table twice in the from clause with different aliases. This will give you different

How to JOIN two FKs from a table to another table?

混江龙づ霸主 提交于 2019-12-12 04:46:51
问题 I have a table of many-to-many relationships as: Table relationship : relationship_id, first_user REFERENCES users(user_id), second_user REFERENCES users(user_id), Table users : user_id, other user information To read friends of a given user (which can be in first_user or second_user ), I need to JOIN two tables ( relationships and users ), but two columns of the table relationships are FK Question 1: How can I JOIN two tables to use both FKs? I have a table for pending requests which is

CakePHP-2.0 How can i get the username from $allposts=$this->paginate('Post'); where user_id is foreign key of posts table?

喜欢而已 提交于 2019-12-12 04:35:45
问题 I'm using CakeDC-Users plugin. <?php class Post extends AppModel { public $useTable='posts'; public $belongsTo = array('User'); public $hasMany=array('Comment'); } I had to use paginate: $allposts=$this->paginate('Post'); I can get the user_id in this way: foreach ($allposts as $post) { debug($post['Post']['user_id']); But i need the username not the user_id. How can i get username? 回答1: The containble feature of CakPHP hides all associated models by default: http://book.cakephp.org/2.0/en

How to insert data into a child table in mysql

社会主义新天地 提交于 2019-12-12 04:13:11
问题 I have 2 tables, an articles and a summaries table. The summaries table is a child of the articles table and I would like to add data into the summaries table through the articles table. The sql for the summaries table is as follows: CREATE TABLE summaries( summary_id INT NOT NULL AUTO_INCREMENT, article_id INT, summary TEXT, PRIMARY KEY(summary_id), FOREIGN KEY(article_id) REFERENCES articles(article_id) )ENGINE=INNODB; How do I add a summary into the summaries table and have the article_id

SQL inner join performance issues

一曲冷凌霜 提交于 2019-12-12 03:23:42
问题 I have a WordPress installation and I'm making a custom plugin. My plugin stores a good bit of usermeta. I'm using an inner join to combine data from the "users" and "usermeta" table at which point I spit the result back to my PHP script. Here's what my query looks like: select # Users table stuff users.ID, users.user_email, users.display_name, # Meta stuff first_name.meta_value as first_name, last_name.meta_value as last_name, phone_number.meta_value as phone_number, country.meta_value as

Self join returning more rows than expected

时光怂恿深爱的人放手 提交于 2019-12-12 03:18:49
问题 I am new to self joins and made up the following example table: +-----------+-------------+ | name | location | +-----------+-------------+ | Robert | Guadalajara | | Manuel | Guadalajara | | Dalia | Guadalajara | | Alejandra | Guadalajara | | Luis | Guadalajara | | Monica | Guadalajara | | Claudia | Guadalajara | | Scartlet | Guadalajara | | Sergio | Guadalajara | | Rick | Mexico City | | Rene | Mexico City | | Ramon | Culiacan | | Junior | Culiacan | | Kasandra | Culiacan | | Emma |

mysql: update with subquery,

只愿长相守 提交于 2019-12-12 02:59:03
问题 I have an update query with a select statement, which separately works. It's using it for the update that's not working. update data set data.id = (select nid from node inner join data on node.title = data.name); I get the error "You can't specify target table 'data' for update in FROM clause" So, after digging around, I found that I could write include another select statement: update data set data.id = (select nid from(select nid from node inner join data on node.title = data.name) AS