I have table obs
+--------+-------+
| obs_id | name |
+--------+-------|
| 101 | mics |
| 102 | jan |
+--------+-------+
I have table monitoring
+--------+--------+---------+
| mon_id | obs_id | code_id |
+--------+--------+---------+
| 1 | 101 | 201 |
| 2 | 101 | 201 |
| 3 | 101 | 202 |
| 4 | 102 | 201 |
| 5 | 102 | 202 |
+--------+--------+---------+
I have table code
+--------+-----------+
|code_id | code_name |
+--------+-----------|
| 201 | node |
| 202 | java |
| 203 | c++ |
+--------+-----------+
Query result
+--------+--------+---------+-----------+
| obs_id | name | code_id | code_name |
+--------+--------+---------+-----------+
| 101 | mics | 201 | node |
| 102 | jan | 201 | node |
+--------+--------+---------+-----------+
Can someone give me a proper mysql query to come up my result.
select A.obs_id, A.name, M.code_id, C.code_name from obs as A
left join monitoring as M on M.obs_id = A.obs_id
left join code as C on C.code_id = M.code_id
The return of my query is more than 2 or it is not what I want as a result.
As per your result ,its look like you want to results for only 'node' .
Then query will be look like below :
CREATE PROCEDURE GETDATA
AS
#CodeId int=0
BEGIN
select DISTINCT A.obs_id, A.name, M.code_id, C.code_name from obs as A
left join monitoring as M on M.obs_id = A.obs_id
left join code as C on C.code_id = M.code_id WHERE c.code_id=#CodeId
END
Now ,you need to only pass codeID into stored Procedure ,it will be return an output based on CodeID . Its look like dynamic .
The above query will give result as you required.
If you want to get distinct data for all code, then just remove where condition.
Thanks .
Related
i am watching a tutorial. There is a code which i don't understand what is supposed to do.
$sql = 'SELECT p.*,
a.screen_name AS author_name,
c.name AS category_name
FROM
posts p
LEFT JOIN
admin_users a ON p.author_id = a.id
LEFT JOIN
categories c ON p.category_id = c.id
WHERE
p.id = ?';
I read about the left joins but i didn't understand them. Can somebody please explain me the code i shared.
Thanks in advance!
Imagine you have two tables. One that stores the information about the programmers on your website, and the other table that keeps track of their online purchases.
PROGRAMMERS Table
+--------------------------------------------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Desire | 32 | 123 fake s| 3000.00 |
| 2 | Jamin | 25 | 234 fake s| 2500.00 |
| 3 | Jon | 23 | 567 fake s| 2000.00 |
| 4 | Bob | 30 | 789 fake s| 1500.00 |
| 5 | OtherGuy | 31 | 890 fake s| 1000.00 |
| 6 | DudeMan | 32 | 901 fake s| 500.00 |
+--------------------------------------------+
PURCHASES Table
+---------------------------------------------+
| ORDER_ID | PROG_ID | DATE | PRICE |
+-------------+---------+---------------------|
| 1 | 1 | 1-1-2017 | 100 |
| 2 | 2 | 1-2-2017 | 200 |
| 3 | 6 | 1-3-2017 | 300 |
+---------------------------------------------|
You decide you need to make a new table to consolidate this information to a table that contains
certain columns you want.
For example, you figure it would be nice for shipping purposes to have a table
that has the ID, the NAME, the PRICE, and the DATE columns.
Currently, the tables we have don't display all of that in a single table.
If we were to LEFT JOIN these tables, we would end up filling the desired columns
with NULL values where there is no information to join.
SELECT ID, NAME, PRICE, DATE
FROM PROGRAMMERS
LEFT JOIN PURCHASES
ON PROGRAMMERS.ID = PURCHASES.PROG_ID;
Notice that I'm selecting the columns I want from the starting table, then joining the right table
even though there might be missing information.
RESULTING TABLE
+-------------------------------------+
| ID | NAME | PRICE | DATE |
+----+----------+-----------------+---+
| 1 | Desire | 100 | 1-1-2017 |
| 2 | Jamin | 200 | 1-2-2017 |
| 3 | Jon | NULL | NULL |
| 4 | Bob | NULL | NULL |
| 5 | OtherGuy | NULL | NULL |
| 6 | DudeMan | 300 | 1-3-2017 |
+-------------------------------------+
For a visual representation of the difference between SQL JOINs check out
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins .
I have two resultsets A and B
The table A looks like this
+------+---------+------------+
| a_id | item no | total sold |
+------+---------+------------+
| 1 | 101 | 23 |
| 2 | 102 | 34 |
| 4 | 405 | 54 |
| 5 | 506 | 65 |
| 6 | 104 | 23 |
+------+---------+------------+
The table B looks like this
+------+---------+----------+
| b_id | item no | location |
+------+---------+----------+
| 1 | 101 | A1 |
| 2 | 102 | A2 |
| 3 | 103 | A3 |
| 4 | 104 | A4 |
+------+---------+----------+
I want to achieve the output as follows
+------+---------+------------+----------+
| a_id | item no | total sold | location |
+------+---------+------------+----------+
| 1 | 101 | 23 | A1 |
| 2 | 102 | 34 | A2 |
| 4 | 405 | 54 | NULL |
| 5 | 506 | 65 | NULL |
| 6 | 104 | 23 | A4 |
+------+---------+------------+----------+
I want to append the column 'LOCATION' to table A and display the location value for each item no which is present in table B. If the ITEM NO in table A does not have a location value, then the LOCATION value has to be NULL (i.e EMPTY).
Since I am a beginner, I don't know how to achieve it. I tried using UNION but I failed to write a proper query
What you are looking for is a Join. Specifically a Left Outer Join so you get all of the results from your Left table (TableA) and only those results from your right table (TableB) that match.
When specifying a join, you use the ON clause to tell the DB which fields are related between the tables:
SELECT
a_id,
a.itemno,
a.totalsold,
b.location
FROM
tableA
LEFT OUTER JOIN tableB ON
tableA.itemno = tableB.itemno
You should use LEFT JOIN (left cause you want to get nulls when the record is not found in the second table)
SELECT a.a_id, a.itemno, a.totalsold, b.location
FROM tableA AS a LEFT JOIN tableB AS b ON
a.itemno = b.itemno
I have many to many relation problem in this scenario
Table : customer_master
+---------+-----------+
| cust_id | cust_code |
+---------+-----------+
| 1 | S00333 |
+---------+-----------+
Table : customer_supply_chain
+----------------+-----------+-------------------+
| supply_cust_id | cust_code | supplye_cust_code |
+----------------+-----------+-------------------+
| 1 | S00333 | HI00001 |
| 2 | S00333 | MA00010 |
+----------------+-----------+-------------------+
Table : customer_vehicle
+----------------+-----------+------------------+
| cust_vehicl_id | cust_code | vehicle_model_id |
+----------------+-----------+------------------+
| 1 | S00333 | 161 |
| 2 | S00333 | 162 |
+----------------+-----------+------------------+
Mysql Query is :
SELECT
cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM customer_master cm
LEFT JOIN customer_supply_chain csc
ON csc.cust_code = cm.cust_code
LEFT JOIN customer_vehicle cv
ON cv.cust_code = cm.cust_code
WHERE cm.cust_code = 'S00333'
result of query
+------------------+------------------+
| vehicle_model_id | supply_cust_code |
+------------------+------------------+
| 161 | HI00001 |
| 162 | HI00001 |
| 161 | MA00010 |
| 162 | MA00010 |
+------------------+------------------+
our desire output is
+------------------+------------------+
| vehicle_model_id | supply_cust_code |
+------------------+------------------+
| 161 | HI00001 |
| 162 | MA00010 |
+------------------+------------------+
The problem is in every row is repeated by supply_cust_code and vehicle_model_id,
We try with distinct and group by and other fixes (show in stack overflow) but not work,
If we use group by so we can not get all data and we don'T want to use group_concat , we cant rectify the problem ,
We just try to make getting all row but not repeated
I don't know why you are getting those duplicates, because the sample data does not support it, but you can either try using SELECT DISTINCT:
SELECT DISTINCT cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM ...
Or you can try grouping on the model ID and supply customer code:
SELECT cv.vehicle_model_id,
csc.supply_cust_code AS suppl_code
FROM customer_master cm
LEFT JOIN customer_supply_chain csc
ON csc.cust_code = cm.cust_code
LEFT JOIN customer_vehicle cv
ON cv.cust_code = cm.cust_code
WHERE cm.cust_code = 'S00333'
GROUP BY cv.vehicle_model_id,
csc.supply_cust_code
Today I have been asked a question by an interviewer that stated
we have three tables named as table A, B, and C.
Those tables are like this
A B C
------------------ -------------------------- ----------------------------
| ID | ProjectID | | ID | LocationID | aID | | ID | points | LocationID |
------------------ -------------------------- ----------------------------
| 1 | 15 | | 1 | 131 | 1 | | 1 | 123333 | 131 |
| 2 | 15 | | 2 | 132 | 1 | | 2 | 123223 | 132 |
| 3 | 15 | | 3 | 133 | 1 | | 3 | 522 | 211 |
| 4 | 12 | | 4 | 134 | 2 | | 4 | 25 | 136 |
------------------ | 5 | 136 | 2 | | 5 | 25 | 133 |
| 6 | 137 | 3 | | 6 | 25 | 134 |
| 7 | 138 | 1 | | 7 | 25 | 135 |
-------------------------- ----------------------------
now he told me to write a query that sums the points of those locations whose project is 15.
First i wrote the query to get ID's from table A like this
SELECT ID from A where projectID = 15
then i pass this result in table b query just like this
SELECT LocationID FROM B WHERE aID IN ( SELECT ID from A where projectID = 15 )
Then i calculate the sum of these locations just like this
SELECT SUM(points) from C where LocationID IN(SELECT LocationID FROM B WHERE aID IN ( SELECT ID from A where projectID = 15))
My Result is fine and query is correct. But he rejected my answer by saying that this nested IN Clause will slow down the whole process as when we have thousands of records.
Then he gave me another chance to review my answer but i couldn't figure it out.
Is there anyway to optimize this or is there some other way to do the same.
Any help? Thanks
Try this it may solve your problem.
Select SUM(C.points) FROM C JOIN B ON C.LocationID = B.LocationID JOIN A ON B.aID = A.ID where A.ProjectID = 15 GROUPBY A.ProjectID
Try with this....i hope it will work
select sum(c.points) as sum_points
from A a,B b,C c where
a.ID=b.aID and
b.LocationID=c.LocationID
and a.projectID=15
I have 2 tables.
Phonebook:
_________________________________
| id | Photo | Name | Number |
|---------------------------------|
| 1 | abc.jpg | John | 123-45-67 |
|---------------------------------|
| 2 | def.jpg | Sam | 482-34-00 |
|_________________________________|
History:
____________________________________
| id | Name | Date | Type |
|------------------------------------|
| 24 | John | 17.03.2014 | Incoming |
|------------------------------------|
| 25 | Sam | 18.03.2014 | Incoming |
|------------------------------------|
| 26 | Sam | 19.03.2014 | Outgoing |
|____________________________________|
I need to get all columns from History table where id = $id (25 in my case) and get Image column from Phoneboock where History.Name = Phonebook.Name (Sam in my case). Below is the result that I want to get:
______________________________________________
| id | Name | Date | Type | Image |
|----------------------------------------------|
| 25 | Sam | 18.03.2014 | Incoming | def.jpg |
|______________________________________________|
It seems your problem is the SQL to get your data, so use this:
SELECT History.*, Phonebook.Photo
FROM History INNER JOIN Phonebook ON Phonebook.Name = History.Name
AND History.id = $id
You can use JOIN as follows:
SELECT * FROM History INNER JOIN Phonebook WHERE History.name = Phonebook.name
That will give you a join of all the rows. You can customize it more to get what you want. Look at JOIN here for more info
If you are looking for the SQL Select statement, it would look something like this:
SELECT HISTORY.*, PHONEBOOK.Photo FROM HISTORY, PHONEBOOK
WHERE HISTORY.Name = PHONEBOOK.Name AND HISTORY.id='25'