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
Related
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | |
| 2 | 101 | |
| 3 | 101 | |
| 4 | 102 | |
| 5 | 102 | |
| 6 | 103 | |
|-------------------------------------------|
Table Region
|---------------------------|
| id | region_id |
| 1 | A101 |
| 2 | A102 |
| 3 | A301 |
| 4 | B101 |
|---------------------------|
Table Info
|--------------------------------------|
| id | address |
| 100 | A101 |
| 101 | A102;A101 |
| 102 | A301;B101 |
| 103 | B101 |
|--------------------------------------|
I want to map info table's id and fill table Mapper. Above sample, my desired output is to make table Mapper to
Table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 1 | 100 | 1 |
| 2 | 101 | 1 |
| 3 | 101 | 2 |
| 4 | 102 | 3 |
| 5 | 102 | 4 |
| 6 | 103 | 4 |
|-------------------------------------------|
this. reg_fk means table Region's ID, and I select reg_fk based on table Info. table Mapper already exists based on number of table Info's address, so all I have to do is read Info's address and put proper ID to Mapper.
This SQL is what I tried:
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id
set mapper.reg_fk = (
case
when info_fk = info.id then
(select region_id
from region_info
where region_info.info_id = info.id
)
end
)
where region_info.info_id = info.id;
It emits error, and I understood that I made a mistake that inside setting up mapper.reg_fk, there's multiple rows which I cannot set one 'mapper.reg_fk row' to multiple rows. But in this SQL sentence, if I join info and mapper on info_fk = info.id, then I should return multiple rows too. like in this example, info_fk = 100 doesn't make any error but info_fk = 101 then update 101 returns 2 rows. And also inside case sentence, I also get 2 rows (region_id 1 and 2) so it would be like
with join restriction, what to update in table Mapper
|-------------------------------------------|
| id | info_fk | reg_fk |
| 2 | 101 | |
| 3 | 101 | |
|-------------------------------------------|
what appears in case sentence
|--------------------|
| reg_fk |
| 1 |
| 2 |
|--------------------|
I want to properly update reg_fk to table Mapper. How do I do this?
Here you can simply build my example data and test my code (which is not working right now).
http://sqlfiddle.com/#!9/8c0c7d/4
Thank you.
with region_info (region_id, info_id) as (
select region.id as region_id, info.id as info_id
from Region region
inner join Info info on
info.address like concat(concat('%', region.region_id), '%')
)
update Mapper mapper inner join Info info
on info_fk = info.id inner join region_info on info.id = region_info.info_id
set mapper.reg_fk = (
(select region_id
from region_info
where region_info.info_id = info.id
LIMIT 1
)
)
last "where region_info.info_id = info.id;" is out of subquery. region_info is in subquery. so it can't find region_info. although you use "with" clause, if it is in "from" clause, you can't find it.
and you can use "LIMIT 1" or "any"(before subquery).
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 .
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'
I have 3 tables to join and need some help to make it work, this is my schema:
donations:
+--------------------+------------+
| uid | amount | date |
+---------+----------+------------+
| 1 | 20 | 2013-10-10 |
| 2 | 5 | 2013-10-03 |
| 2 | 50 | 2013-09-25 |
| 2 | 5 | 2013-10-01 |
+---------+----------+------------+
users:
+----+------------+
| id | username |
+----+------------+
| 1 | rob |
| 2 | mike |
+----+------------+
causes:
+--------------------+------------+
| id | uid | cause | <missing cid (cause id)
+---------+----------+------------+
| 1 | 1 | stop war |
| 2 | 2 | love |
| 3 | 2 | hate |
| 4 | 2 | love |
+---------+----------+------------+
Result I want (data cropped for reading purposes)
+---------+-------------+---------+-------------+
| id | username | amount | cause |
+---------+-------------+---------+-------------+
| 1 | rob | 20 | stop war |
| 2 | mike | 5 | love |
+---------+-------------+-----------------------+
etc...
This is my current query, but returns double data:
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid)
EDIT: fixed sql schema on fiddle
http://sqlfiddle.com/#!2/0e06c/1 schema and data
How I can do this?
It seems your table's model is not right. There should be a relation between the Causes and Donations.
If not when you do your joins you will get duplicated rows.
For instance. Your model could look like this:
Donations
+--------------------+------------+
| uid | amount | date | causeId
+---------+----------+------------+
| 1 | 20 | 2013-10-10 | 1
| 2 | 5 | 2013-10-03 | 2
| 2 | 50 | 2013-09-25 | 3
| 2 | 5 | 2013-10-01 | 2
+---------+----------+------------+
causes:
+----------------------+
| id | cause |
+---------+------------+
| 1 | stop war |
| 2 | love |
| 3 | hate |
+---------+------------+
And the right query then should be this
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.id = tti.causeId)
Try this
SELECT CONCAT(i.username ,' ',i.first_name) `name`,
SUM(tti.amount),
t.cause AS tag_name
FROM users i
LEFT JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid)
GROUP BY i.id
Fiddle
You need to match the id from both the users and causes table at the same time, like so:
SELECT i.*, t.cause as tag_name
FROM users i
INNER JOIN donations tti ON (tti.uid = i.id)
INNER JOIN causes t ON (t.uid = tti.uid and t.id = i.id)
Apologies for formatting, I'm typing this on a phone.
I'm facing a problem here :
I have two tables :
A users table :
+----+---------------+----------+
| id | username | company |
+----±---------------±----------+
| 1 | John | 0 |
| 2 | Jack | 0 |
| 3 | Casimir | 0 |
±----±---------------±----------±
A orders table :
+----+---------------+----------+--------+
| id | date | iduser | status |
+----±---------------±----------+--------+
| 1 | 2012-05-28 | 1 | 1 |
| 2 | 2012-05-25 | 1 | 1 |
| 3 | 2012-04-28 | 2 | 1 |
| 4 | 2012-03-28 | 1 | 1 |
| 5 | 2012-02-28 | 2 | 0 |
±----±---------------±----------±--------+
What I'm trying to do is to get a result like this :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
| Casimir | 0 | NULL |
±----------±---------------±-------------±
Here's the request I have for the moment :
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
INNER JOIN orders ON u.id = o.iduser
WHERE o.status = 1
GROUP BY u.id
This request gives me a result like :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
±----------±---------------±-------------±
As you can see, the user Casimir is not shown as he made no order. How can I modify my request to get the result I need please ?
Thanks !
A LEFT JOIN or LEFT OUTER JOIN will include all rows of the inital table, including those where there is no match in the joined-to table
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
LEFT OUTER JOIN orders o ON u.id = o.iduser AND o.status = 1
GROUP BY u.id
You need to use an OUTER JOIN instead of your current INNER JOIN.
Have a look at Jeff's post here to see how they differ:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html