Getting the filenames record - mysql

I have case to get filenames from table foto
for the firts table
tb_induk
----------------
id_induk | nomor_induk | id_burung | hen | cock
25 | IND003 | 2 | 91442 | 30441
26 | IND004 | 2 | 10464 | 40020
note : column "hen" and "cock" is "id_data" from birds
second table:
tb_foto
----------------------
id_foto | id_data | filename
566 |91442 |4b584a8cd6cb98d4b4d9614abb223034.JPG
567 |91442 |b526318215b5c12bf79b3284d8bd5db7.JPG
568 |30441 |4db159080e0cefb4fd9a2862502ab0f5.JPG
570 |10464 |4216d01abb3bdce90bd72e40ef1d593b.JPG
571 |40020 |6c5b28bffd79fc661f44733c04d12446.JPG
The query is:
Query
-----------------------
"select
tb_induk.id_induk as id_induk,
tb_induk.nomor_induk as nomor_induk,
case WHEN tb_foto.id_data=tb_induk.Hen THEN tb_foto.filename END as filenamehen,
case WHEN tb_foto.id_data=tb_induk.Cock THEN tb_foto.filename END as filenamecock
from tb_induk INNER JOIN tb_foto ON
tb_induk.Hen=tb_foto.id_data || tb_induk.Cock=tb_foto.id_data
where id_burung=$id_burung GROUP BY tb_induk.id_induk"
Result
Result
------------------------------
id_induk | nomor_induk | id_burung | hen | cock
25 | IND003 | 2 | 4216d01abb3bdce90bd72e40ef1d593b.JPG | NULL
26 | IND004 | 2 | NULL | 207b21895c556bc5fada1ead8ec34d06.JPG

I think you can just join tb_foto twice to pull in the filenames for hens and cocks, for each tb_induk value:
SELECT t1.tb_induk,
t1.nomor_induk,
t1.id_burung,
COALESCE(tf1.filename, 'NA') AS hen,
COALESCE(tf2.filename, 'NA') AS cock
FROM tb_induk t1
LEFT JOIN tb_foto tf1
ON t1.hen = tf1.id_data
LEFT JOIN tb_foto tf2
ON t1.cock = tf2.id_data

Related

MySQL - Join two tables in a view, take rows from one and turn into columns

There is a Request Table and for each request, there is two guarantors required. So I made a table for request details and another table for guarantors details.
Now I need a view that included all requests and all guarantors name as I described below.
Could anyone please help me to resolve this..
I need MySQL Code to create this output. (for creating a PHP page)
These are my tables.
table tblReq
|-------------------------------------------|
| ReqID | ReqAmount | ReqDate |
|-------------------------------------------|
| 1001 | 25000 | 2017-10-21 |
| 1002 | 21000 | 2017-10-10 |
| 1003 | 28000 | 2017-10-15 |
| 1004 | 20000 | 2017-10-22 |
|-------------------------------------------|
table tblGuarantors
|------------------------------------------|
| G_ID | ReqID | G_Name | G_Contact |
|------------------------------------------|
| 10001 | 1001 | Ramu | 123456 |
| 10002 | 1001 | Deepu | 123456 |
| 10003 | 1002 | Mary | 123456 |
| 10004 | 1002 | James | 123456 |
| 10005 | 1003 | Muhad | 123456 |
| 10006 | 1003 | Mannu | 123456 |
|------------------------------------------|
Required Output
|------------------------------------------|
| ReqID | ReqAmount | G1_Name | G2_Name |
|------------------------------------------|
| 1001 | 25000 | Ramu | Deepu |
| 1002 | 21000 | Mary | James |
| 1003 | 28000 | Muhad | Mannu |
| 1004 | 20000 | Null | Null |
|------------------------------------------|
Can you anyone help me?
I tried this SQL, but it doesn't work as I assumed.
SELECT
r.ReqID,
r.ReqAmount,
g1.G_Name,
g2.G_Name
from
(tblReq as r LEFT JOIN tblGuarantors as G1 on g1.ReqID = r.ReqID)
LEFT JOIN tblGuarantors as G2 on g2.ReqID = r.ReqID
A little help will be appreciated.
Thanks in advance....
You can use the following query:
SELECT r.ReqID, r.ReqAmount,
MIN(G_Name) AS G1_Name,
MAX(G_Name) AS G2_Name
FROM tblReq AS r
LEFT JOIN tblGuarantors AS g ON r.ReqID = g.ReqID
GROUP BY r.ReqID, r.ReqAmount
You have to amend the query in case of request records related to just one guarantor record.
Demo here
Edit:
To get all fields from guarantors table you need a different query:
SELECT r.ReqID, r.ReqAmount,
g1.G_ID AS G1_ID, g1.G_Name AS G1_Name,
g2.G_ID AS G2_ID, g2.G_Name AS G2_Name
FROM tblReq AS r
LEFT JOIN tblGuarantors AS g1
ON r.ReqID = g1.ReqID
LEFT JOIN tblGuarantors AS g2
ON r.ReqID = g2.ReqID
WHERE (g1.G_ID < g2.G_ID) OR (g1.G_ID IS NULL)
The first LEFT JOIN gets the details of the guarantor having the smallest G_ID value, whereas the second LEFT_JOIN gets the details of the other guarantor. You also need predicate
g1.G_ID IS NULL
so as to return request records with no guarantors as well.
Demo here
I have written code in notepade, maybe you shoud be edit it, but i think it is will be worked. Sorry for my English.
SELECT
tblReq.ReqID,
tblReq.ReqAmount,
(SELECT G_Name FROM tblGuarantors WHERE tblGuarantors.ReqID = tblReq.ReqID ORDER by G_ID ASC LIMIT 1) as G1_Name,
(SELECT G_Name FROM tblGuarantors WHERE tblGuarantors.ReqID = tblReq.ReqID ORDER by G_ID DESC LIMIT 1) as G2_Name
FROM tblReq
Try this
select tblReq.ReqID, tblReq.ReqAmount, MIN(tblGuarantors.G_Name) AS G1_Name,
MAX(tblGuarantors.G_Name) AS G2_Name
from tblGuarantors
left join tblReq on tblReq.ReqID=tblGuarantors.ReqID;

Using left join with min

I am trying to connect two tables with left join and a date.
My SQL Query
SELECT
ord.`ordernumber` bestellnummer,
his.`change_date` zahldatum
FROM
`s_order` ord
LEFT JOIN
`s_order_history` his ON ((ord.`id`=his.`orderID`) AND (ord.`cleared`=his.`payment_status_id`)) #AND MIN(his.`change_date`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\'' ;
s_order
+----+---------------------+---------+-------------+
| id | ordertime | cleared | ordernumber |
+----+---------------------+---------+-------------+
| 1 | 2014-08-11 19:53:43 | 2 | 123 |
| 2 | 2014-08-15 18:33:34 | 2 | 125 |
+----+---------------------+---------+-------------+
s_order_history
+----+-------------------+-----------------+---------+---------------------+
| id | payment_status_id | order_status_id | orderID | orderID change_date |
+----+-------------------+-----------------+---------+---------------------+
| 1 | 1 | 5 | 1 | 2014-08-11 20:53:43 |
| 2 | 2 | 5 | 1 | 2014-08-11 22:53:43 |
| 3 | 2 | 7 | 1 | 2014-08-12 19:53:43 |
| 4 | 1 | 5 | 2 | 2014-08-15 18:33:34 |
| 5 | 1 | 6 | 2 | 2014-08-16 18:33:34 |
| 6 | 2 | 6 | 2 | 2014-08-17 18:33:34 |
+----+-------------------+-----------------+---------+---------------------+
Wanted result:
+-------------+---------------------+
| ordernumber | change_date |
+-------------+---------------------+
| 123 | 2014-08-11 22:53:43 |
| 125 | 2014-08-17 18:33:34 |
+-------------+---------------------+
The problem I have is getting only the date, where the cleared/payment_status_id value has been changed in s_order. I currently get all dates where the payment_status_id matches the current cleared value, but I only need the one, where it happend first.
This is only an excerpt of the actually query, since the original is a lot longer (mostly more left joins and a lot more tables).
You can group data by ordernumber
SELECT
ord.`ordernumber` bestellnummer,
MIN(his.`min_change_date`) as zahldatum
FROM
`s_order` ord
LEFT JOIN
`s_order_history` his ON ((ord.`id`=his.`orderID`) AND (ord.`cleared`=his.`payment_status_id`)) #AND MIN(his.`change_date`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\''
GROUP BY
ord.`ordernumber`;
or you can group data in a subquery:
SELECT
ord.`ordernumber` bestellnummer,
his.`min_change_date` zahldatum
FROM
`s_order` ord
LEFT JOIN (
SELECT
orderID, payment_status_id, MIN(change_date) as min_change_date
FROM
s_order_history
GROUP BY
orderID, payment_status_id
) his ON (ord.`id` = his.`orderID` AND ord.`cleared` = his.`payment_status_id`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\'';
Try this:
select s_order.ordernumber, min(s_order_history.change_date)
from s_order left join s_order_history
on s_order.id = s_order_history.orderID
and s_order.cleared = s_order_history.payment_status_id
group by s_order.order_id
SELECT ord.`ordernumber` bestellnummer,
MIN( his.`change_date` ) zahldatum
...
GROUP BY ord.`ordernumber`
MIN is an aggregate function so you can't use it in a JOIN straight up like you've tried above. You also are not comparing it to a value in your JOIN.
You'll want to do something like:
his.`change_date` = (SELECT MIN(his.`change_date`) FROM s_order_history where ord.`id` = his.`orderID`)
in your JOIN.

MySQL: joining tables with multi results rows to one row

I have 4 tables:
secu_content
| id | created | modified |
| 910 | 26/12/1982 | 28/12/1984 |
| 911 | 24/12/1982 | 25/12/1984 |
secu_data
| element_id | field_id | data |
| 1 | 1 | 25/12/1984 |
| 2 | 1 | 26/12/1984 |
| 3 | 1 | 27/12/1984 |
| 4 | 1 | 25/12/1984 |
| 4 | 2 | google.com |
secu_elements
| id | item_id |
| 1 | 891 |
| 2 | 711 |
| 3 | 204 |
| 4 | 911 |
secu_fields
| id | type |
| 1 | date |
| 2 | input |
Table secu_content, contains many articles, where the id is the article id.
The other 3 tables gives additional information and I want to join them.
I want to get results that includes all secu_content rows and all the columns + calc_date + calc_link
calc_date <- the data column from secu_data where field_id=1 (see secu_fields)
calc_link <- the data column from secu_data where field_id=2 (see secu_fields)
The problem is that I get 2 rows where secu_content id=911 (one row with the correct calc_date and second row with the correct calc_link), and I need one row with both.
This is my SQL:
SELECT a.id
, a.created
, a.modified
, fe.item_id AS calc_date_item_id
, fd.data AS calc_date
, CASE WHEN fd.data IS NOT NULL AND ff.type = "date" THEN fd.data
WHEN a.modified = '0000-00-00 00:00:00' THEN a.created ELSE a.modified
END as calc_date
, CASE WHEN fd.data IS NOT NULL AND ff.type = "input" THEN fd.data
END as calc_link
FROM secu_content AS a
LEFT
JOIN secu_fieldsandfilters_elements AS fe
ON fe.item_id = a.id
AND fe.content_type_id=1
LEFT
JOIN secu_fieldsandfilters_data AS fd
ON fd.element_id = fe.id
LEFT
JOIN secu_fieldsandfilters_fields as ff
ON ff.id = fd.field_id
ORDER BY a.id DESC;
Thanks in advance
Israel
Fast and dirty solution is to use second join to secu_data like that (simplified, add logic you need)
SELECT id, d1.data as `calc_date`, d2.data as `calc_link`
FROM secu_content
LEFT JOIN secu_data d1 ON secu_content.id = d1.element_id AND field_id = 1
LEFT JOIN secu_data d2 ON secu_content.id = d2.element_id AND field_id = 2

Get all data where 2 string from 2 table are same

I have 2 tables :
tb_doc :
===========================================
| id | document_title | document_summary |
===========================================
| 1 | a data mining | document_summary1 |
| 2 | B | document_summary2 |
===========================================
tb_wrap :
============================================
| id | data_url | data_title | doc_url |
===========================================
| 1 |data_url1 | B | doc_url1 |
| 2 |data_url2 | a data mining | doc_url2 |
============================================
I wanna join data from 2 table where document_title = data_title (match 2 strings), so I will get the result like :
data_title <href data_url>
doc_url
document_summary
here's the query :
SELECT * from tb_wrap as a
JOIN (
SELECT document_title, document_summary from tb_doc) as b`
ON a.data_title LIKE b.document_title
How to get them? thank you :)
The JOIN condition should be an = rather than a LIKE. I see no need to join against a subquery. This is just a rudimentary INNER JOIN.
SELECT
tb_wrap.data_url,
tb_wrap.data_title
tb_wrap.doc_url,
tb_doc.document_summary
FROM
tb_doc
INNER JOIN tb_wrap ON tb_doc.document_title = tb_wrap.data_title

Merging Corresponding MySQL Records

I have a MySQL table called "objecttable" that has the following structure and data in it. (The data is just a sequence, there is a whole lot more).
ID | Name | posX | posY | posZ |rotX | rotY | rotZ | rotW |
3562 | LODpmedhos1_LAe | 2062 | -1703 | 16 | 0 | 45 | 22 | 1 |
3559 | LODpmedhos5_LAe | 2021 | -1717 | 15 | 0 | 45 | 34 | 1 |
3561 | LODpmedhos3_LAe | 2021 | -1717 | 15 | 0 | 45 | 34 | 1 |
I want to figure out which records have the same posX, posY, posZ, rotX, rotY and rotZ values and insert them into a table called "matchtable", and in the end I want it to look like this (I have the table structure ready)
ID1 | Name | ID2 | Name |
3559 | LODpmedhos5_LAe | 3561 | LODpmedhos3_LAe|
I'd appreciate if someone could give me the correct SQL query for it. I don't have more than two matching coordinates and not all coordinates match.
Sorry if the table representations suck, I'll try to make a HTML table if necessary.
Thanks!
This query will do the trick, but the number of results might be a LOT more than required. For example, if there are 5 rows satisfying your query, then the results will be 20( = n*(n-1) ) in number.
SELECT ot.ID AS ID1, ot.Name AS Name1, ot2.ID AS ID2, ot2.Name AS Name
FROM objecttable ot
JOIN objecttable ot2
ON ot.ID > ot2.ID
AND ot.posX = ot2.posX
AND ot.posY = ot2.posY
AND ot.posZ = ot2.posZ
AND ot.rotX = ot2.rotX
AND ot.rotY = ot2.rotY
AND ot.rotZ = ot2.rotZ
EDIT
In reply to lserni's comment:
ON ot.ID <> ot2.ID
The above condition is there to remove the result like:
ID1 | Name | ID2 | Name |
3559 | LODpmedhos5_LAe | 3559 | LODpmedhos5_LAe|
try this:
-- insert into matchtable -- uncomment to insert the data
select alias1.Id,
alias1.Name,
alias2.Id
alias2.Name
from objecttable as alias1
join objecttable as alias2
on alias1.posx = alias2.posx
and alias1.posy = alias2.posy
and alias1.posz = alias2.posz
and alias1.roty = alias2.roty
and alias1.roty = alias2.roty
and alias1.rotz = alias2.rotz
and alias1.Id > alias2.Id