I have a database scheme like this:
All I want is to query data from these tables where the one in the middle is a bridgetable.
I want to write a query to get Details from WrapupCodes table for all the WrapupIds that exists in Bridgetable for the same contactId as in CallDetails table.
e.g:
Similarly this is the table I want to get details from
And here is the main table.
Thanks in advance.
It is very basic SQL:
SELECT *
FROM WrapupCodes w
INNER JOIN Contact_Wrapup c ON c.WrapupID = w.WrapupID
INNER JOIN CallDetail d on d.ContactID = c.ContactID
Related
I have two tables A and B. A has unique records while B may have several references to one record in A.
A table -> A.UserID,A.Image1.ID,A.Image2.ID,A.UserName
B table -> B.ImageID,B.ImageURL,ImageDescription
B. Image ID is unique and could have at least two records correspond to Image!ID and Image2ID in table A.
In my query, need to read A.UserName,B.Image1URL and B.Image2URL.
Following SQL query is to read one image. How I could modify this to read both Image1 and Image2 in one SQL query ?.
#"SELECT A.*,B.* FROM A
INNER JOIN B ON B.Image1ID = A.Image1ID
WHERE A.UserID = #Parameter1;";
So in the result, I need following :
UserID
Image1URL
Image2URL
What's the best way to get this done in mySQL ?
You can join same table twice -
SELECT A.UserID, A.UserName, img1.ImageURL, img2.ImageURL
FROM A
INNER JOIN B as img1
ON img1.ImageID = A.Image1ID
INNER JOIN B as img2
ON img2.ImageID = A.Image2ID
WHERE A.UserID = #Parameter1;
I have two tables: user and details table. I want to display the details table in a table format.
user table Structure is:
details Table Structure is:
Result table Structure is:
I have tried this query:
SELECT A.*,B.name,C.name as Interviewer
FROM exitdetail_hrms A
LEFT JOIN hh_tbl_user B ON A.emp_id=B.sno
JOIN hh_tbl_user C
WHERE A.emp_id='12' AND C.sno='13'
But I didn't get exact answer..kindly help me on this..
Thanking You..
I am not sure, because you table description does not match the SQL you used, but i think the following SQL should solve your problem:
SELECT A.*,B.name,C.name as Interviewer
FROM exitdetail_hrms A
LEFT JOIN hh_tbl_user B ON A.emp_id=B.s_no
LEFT JOIN hh_tbl_user C ON A.interviewed = C.s_no
WHERE A.emp_id='12' AND C.sno='13'
I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table
Im currently writing a stored procedure in SQL to print results from multiple tables to find the top ten products purchased, but I am getting the syntax error
"Invalid column name 'ProductID'".
This appears on the 2nd INNER JOIN statement at sod.ProductID
My code below
CREATE PROCEDURE usp_top10ProfitableProducts
AS
BEGIN
SELECT TOP 10 sp.StoreProductID, sup.ProductName, sum(sod.Quantity) AS quantitysold, (sum(sod.Quantity) * sum(sod.unitPrice)) - (sum(sod.Quantity) * sum(sp.costPrice)) AS Profit
FROM SalesOrderDetails sod
INNER JOIN StoreProduct sp ON sp.StoreProductID = sod.StoreProductID
INNER JOIN SupplierProduct sup ON sup.ProductID = sod.ProductID
WHERE Quantity > 0
END
Thanks in advance.
EDIT** Below is also my Entity Relationship diagram. 'ProductID' lives in 'SupplierProduct'
Probably the field does not exist in one of the tables. Another point: group by was missing.
GROUP BY sp.StoreProductID, sup.ProductName
To do a join between two tables the joining column (in above case ProductID) should be available in both tables, otherwise sql will not be able to do the join.
According to the table structure SalesOrderDetails doesn't have a column ProductID which is why it's giving the error, you need to the join on cloumns that exist in the respective tables
I have 2 tables: operations with client processing data and customers with age data. I want create new table, vlookup and add new column Age from customers to operations, but it doesnot work:
CREATE TABLE new_schema.total AS (
SELECT new_schema.operations.Id_check,new_schema.operations.ID_client, new_schema.customers.Age
INNER JOIN Age ON new_schema.operations.ID_client=new_schema.customers.ID_client
);
You have some basic syntax errors. There's no FROM clause to specify the first table, and INNER JOIN must be followed by the table you're joining with, not a column.
And you said you wanted the new table to be named vlookup, but you created total instead.
CREATE TABLE new_schema.vlookup AS (
SELECT o.id_check, o.id_client, c.age
FROM new_schema.operations AS o
INNER JOIN new_schema.customers AS c ON o.id_client = c.id_client
);