inner-join

Combine two tables into one table

帅比萌擦擦* 提交于 2019-12-25 09:28:47
问题 I need to join user table with all user images in one row, like this table user +----+---------------+ |id | name | +----+---------------+ | 1 | Mike | | 2 | Jerry | | .. | ..... | +----+---------------+ table image +------+---------+---------+ | id | user_id | img | +------+---------+---------+ | 1 | 1 | img_1 | | 2 | 1 | img_2 | | 3 | 1 | img_3 | | 4 | 2 | img_4 | | .. | .... | ..... | +------+---------+---------+ I need to generate SQL results like this +------+--------+----------+--------

SQL Count with Inner Join for Single Table

大憨熊 提交于 2019-12-25 08:35:54
问题 I have a table like this: Name Id Amount Name1 1 99 Name1 1 30 Name1 9 120.2 Name2 21 348 Name2 21 21 Name3 41 99 I want to select each name, group them by their id and count the transactions (NOT SUM). So I want the following table: Name Id Count Name1 1 2 Name1 9 1 Name2 21 2 Name3 41 1 I tried this sql: SELECT [Name], [Id] FROM table1 A INNER JOIN ( SELECT [Id], count([Amount]) as 'Count' FROM table1 GROUP BY [Id] ) B ON A.[Id] = B.[Id] But I get the following error: Ambiguous column name

MYSQL Left join A.table and b.table while retaining a.table id

怎甘沉沦 提交于 2019-12-25 08:13:48
问题 MYSQL Left join A.table and b.table while retaining a.table id when there is no b.table match. SELECT * FROM sales_customer a LEFT JOIN sales_contact b ON a.customer_id = b.customer_id ORDER BY CASE WHEN company_name = '' THEN lname ELSE company_name END ASC Gets me this: Array ( [0] => 54 [customer_id] => ) When there is no b.table match. I need this: Array ( [0] => 54 [customer_id] => 29 ) Any suggestions? The solution below worked. Thanks for your help. SELECT *, COALESCE(a.customer_id, 0)

MYSQL Left join A.table and b.table while retaining a.table id

人盡茶涼 提交于 2019-12-25 08:10:05
问题 MYSQL Left join A.table and b.table while retaining a.table id when there is no b.table match. SELECT * FROM sales_customer a LEFT JOIN sales_contact b ON a.customer_id = b.customer_id ORDER BY CASE WHEN company_name = '' THEN lname ELSE company_name END ASC Gets me this: Array ( [0] => 54 [customer_id] => ) When there is no b.table match. I need this: Array ( [0] => 54 [customer_id] => 29 ) Any suggestions? The solution below worked. Thanks for your help. SELECT *, COALESCE(a.customer_id, 0)

SQL equivalent of IN operator that acts as AND instead of OR?

此生再无相见时 提交于 2019-12-25 07:47:03
问题 Rather than describe, I'll simply show what I'm trying to do. 3 tables in 3NF. product_badges is the join table. (The exists sub-query is necessary.) SELECT * FROM shop_products WHERE EXISTS ( // this line cannot change SELECT * FROM product_badges as pb WHERE pb.product_id=shop_products.id AND pb.badge_id IN (1,2,3,4) ); Now this will return all the products that have a badge_id of 1 OR 2 OR 3 OR 4. What I want is to get only the products that meet ALL those values . I tried to do pb.badge

search page that allow user to select between three types using php mysql

我的未来我决定 提交于 2019-12-25 07:04:12
问题 i have a search page that include three types of search and i want to filter the search upon the selecting types type 1 : by newest members type 2 : by specialization type 3 : by name specialization table: specialization_id specialization_name members table : user_id first_name last_name specialization registered_date but the problem that the first type work fine but the second it show all members not the selected specialization the first query for the specialization is for selecting

Update SQL by joining 3 tables

雨燕双飞 提交于 2019-12-25 04:45:09
问题 Using the below table structure i need to insert the Line ID into the approved table, using the location name as the lookup value. How do i go about doing this? I started off using the below code, but it doesn't do much. Not that great on SQL joins so any help is much appreciated. UPDATE dbo.Approved SET dbo.Approved.Groupid=dbo.Lines.ID FROM dbo.Lines,dbo.Approved, dbo.Locations WHERE dbo.Approved.Location = dbo.Locations.Location_Name Approved ID (PK) | Incident | Location | GroupID -------

Oracle SQL compare records within a table

▼魔方 西西 提交于 2019-12-25 04:15:45
问题 I have a table like below: S.No | Item_ID | Item_Revision | Code | -----+---------+---------------+------- 1. | item1 | 0 | xyz | 2. | item2 | 0 | xyz | 3. | item3 | 0 | xyz | 4. | item1 | 1 | | 5. | item2 | 1 | abc | 6. | item3 | 1 | xyz | I need to compare the records in the table to find the differences in code in different revisions of the items. I want the result set as below: | Item_ID | Code_Revision_0 | Code_Revision_1 | | item1 | xyz | | | item2 | xyz | abc | I am not able to

MS Access INNER JOIN most recent entry

旧时模样 提交于 2019-12-25 04:02:49
问题 I'm having some trouble trying to get Microsoft Access 2007 to accept my SQL query but it keeps throwing syntax errors at me that don't help me correct the problem. I have two tables, let's call them Customers and Orders for ease. I need some customer details, but also a few details from the most recent order. I currently have a query like this: SELECT c.ID, c.Name, c.Address, o.ID, o.Date, o.TotalPrice FROM Customers c INNER JOIN Orders o ON c.ID = o.CustomerID AND o.ID = (SELECT TOP 1 ID

fetching names from one table with multiple ids in join

痞子三分冷 提交于 2019-12-25 02:21:49
问题 Employee ID Name 1 John 2 Williams Appointment ID DoctorName Employee_id Team lead 1 willson 1 2 SQL SELECT E.*,A.* FROM Employee as E,Appointment as A WHERE A.Employee_id = E.ID This is my join if i want to fetch Team lead name how we can do that? 回答1: Try this one SELECT E.name FROM Employee E JOIN Appointment A ON A.Employee_id = E.ID WHERE [some condition here] 回答2: IF Team Lead NAME IN Appointment TABLE THEN USE below QUERY: SELECT E.Name AS 'Employee Name',A.DoctorName AS 'Doctor', A