I have two tables like that:
Item
-------------------------------------
|ID | ImageURLID | BannerImageURLID |
-------------------------------------
| 1 | 1 | 2 |
| 2 | 3 | 4 |
| 3 | 5 | 6 |
-------------------------------------
ImageURL
--------------------------------------
|ID | iOSURL | iOSRetinaURL |
--------------------------------------
| 1 | www.test.at | www.testR.at |
| 2 | www.bann.at | www.bannR.at |
| 3 | www.test.at | www.testR.at |
| 4 | www.bann.at | www.bannR.at |
| 5 | www.test.at | www.testR.at |
| 6 | www.bann.at | www.bannR.at |
--------------------------------------
And I want the output look something like this:
-------------------------------------------------------------------------------------
|ID | ImageURLiOS | ImageURLiOSRetina | BannerImageURLiOS | BannerImageURLiOSRetina |
-------------------------------------------------------------------------------------
| 1 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
| 2 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
| 3 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
-------------------------------------------------------------------------------------
I tried some grouping and inner join but nothing really works.
Is it possible to accomplish that with SQL or do I have to do some PHP processing?
select i.id,
u.iOSURL as ImageURLiOS,
u.iOSRetinaURL as ImageURLiOSRetina,
ur.iOSURL as BannerImageURLiOS,
ur.iOSRetinaURL as BannerImageURLiOSRetina
from item i
left join imageurl u on u.id = i.imageurlid
left join imageurl ur on ur.id = i.bannerimageurlid
Related
I am trying to get the below result with 3 tables in SQL and PHPmyAdmin from the last 2 days, can someone please help to reach the output? I appreciate your support in advance.
i am using this query but not able to achive the output.
SELECT calender.date, trade_details.client_code,
sum(trade_details.net_pnl) as trade_Value,
sum(kuber_reports.net_value) as kuber_Value
FROM calender
LEFT JOIN trade_details ON calender.date = trade_details.trade_Date
LEFT JOIN kuber_reports ON calender.date = kuber_reports.trans_Date
WHERE trade_details.client_code = 'GBN10001'
GROUP BY calender.date, trade_details.client_code;
Calendar Table
| ID | date |
| -------- | -------------- |
| 1 | 2022-12-13 |
| 2 | 2022-12-14 |
| 3 | 2022-12-15 |
| 4 | 2022-12-16 |
| 5 | 2022-12-17 |
| 6 | 2022-12-18 |
Kuber_reports Table
| ID | trans_Date | net_Value | client_code
| -------- | -------------- |-------------|-------------
| 1 | 2022-12-14 | 100 | GBN10001
| 2 | 2022-12-14 | -50 | GBN10001
| 3 | 2022-12-14 | 100 | GBN10001
| 4 | 2022-12-15 | 500 | GBN10001
| 5 | 2022-12-16 | 1000 | GBN10001
trade_details Table
| ID | trade_Date | net_pnl | client_code
| -------- | -------------- |-------------|-------------
| 1 | 2022-12-14 | 100 | GBN10001
| 2 | 2022-12-14 | -50 | GBN10001
| 3 | 2022-12-14 | 100 | GBN10001
| 4 | 2022-12-15 | 500 | GBN10001
| 5 | 2022-12-16 | 900 | GBN10001
Output Required
| ID | Calender.date | net_pnl | net_value | client_code | Difference
| -------- | -------------- |-------------|-------------|-------------|------------
| 1 | 2022-12-14 | 150 | 150 | GBN10001 | 0
| 2 | 2022-12-15 | 500 | 500 | GBN10001 | 0
| 3 | 2022-12-16 | 900 | 1000 | GBN10001 |-100
Before joining the tables, you should aggregate the tables Kuber_reports and trade_details individually and then join the resultant tables.
select calender.date, trade_table.client_code,net_value,net_pnl,
differnce= net_pnl-net_value
from calender left join
(select trans_date,client_code, sum(net_value) as net_value
from Kuber_reports group by trans_date,client_code) Kuber_table
on calender.date=Kuber_table.trans_date
left join (select trade_Date, client_code, sum(net_pnl) as net_pnl
from trade_details group by trade_Date ,client_code) trade_table
on calender.date=trade_table.trade_Date
db<>fiddle
To not display null values, it is better to add the following condition
where trade_table.client_code is not null
I have three following tables.
table 1)
| ID_t1 | name1 | price |
| ------ | --------- | ----- |
| 1 | AAA | 100 |
| 2 | ABB | 200 |
| 3 | ACC | 300 |
table 2)
| ID_t2 | name2 |
| ------ | --------- |
| 1 | pig |
| 2 | fog |
| 3 | dog |
table 3)
| ID_t3 | ID_t1 | ID_t2 | quantity |
| ------ | --------- | ----- | -------- |
| 1 | 1 | 1 | 50 |
| 2 | 2 | 1 | 60 |
| 3 | 2 | 2 | 100 |
| 4 | 2 | 3 | 110 |
| 5 | 3 | 2 | 150 |
| 6 | 3 | 3 | 140 |
I want to query data form phpmyadmin to array and i have duplicate values so how to combine duplicate values in object like this. Am new to using PHP.
Am trying to connect two tables Customer and Customer_Usage for getting the result.Tables are showing below.
Table: Customer
+----+-------+-------------+--------+
| id | Ip_Id | Customer_Id | Class |
+----+-------+-------------+--------+
| 1 | 100 | A | First |
| 2 | 100 | B | First |
| 3 | 100 | C | First |
| 4 | 101 | D | First |
| 5 | 101 | E | First |
| 6 | 100 | F | Second |
+----+-------+-------------+--------+
Table: Customer_Usage
+----+-------------+----------------+
| id | Customer_Id | Usage |
+----+-------------+----------------+
| 1 | A | 1245 |
| 2 | B | 4414 |
| 3 | C | 8521 |
| 4 | D | 2314 |
| 5 | E | 521 |
| 6 | F | 5412 |
+----+-------------+----------------+
The condition is while enter a value for search Ip_Id, example for 100 it will give a result like below. How to make a joint for these two tables in Laravel using Mysql
Output result
+-------+-------------+----------------+
| Ip_Id | Customer_Id | Usage |
+-------+-------------+----------------+
| 100 | A | 1245 |
| 100 | B | 4414 |
| 100 | C | 8521 |
| 100 | F | 5412 |
+-------+-------------+----------------+
This is the query am trying.
$result = DB::table('Customer')
->where('Ip_Id','=','100')
->get();
Left join is recommended to achieve what you need.
DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id','Customer_Usage.Usage')
->leftJoin('Customer_Usage', 'Customer_Usage.Customer_Id', '=', 'Customer.Customer_Id')
->where('Customer.Ip_Id',100)
->get();
Use Eloquent Inner Join like:
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', 'Customer_Usage.Usage')
->join('Customer_Usage','Customer.Customer_Id', '=','Customer_Usage.Customer_Id')
->where('Ip_Id', 100)
->get();
You will get the desired output like above.
I have three tables below that shows the student records, subjects and students with subjects.
I would like to ask what is the effective SQL query to show the results below. I can show it using JOIN but not with the format below.
+------+-----------+-----------+-----+----------+-----------+--------+
| Name | Address | Telephone | Sex | Subjects | Teacher | Active |
+------+-----------+-----------+-----+----------+-----------+--------+
| John | somewhere | 12345 | M | | Teacher 1 | YES |
| John | somewhere | 12345 | M | Math | | YES |
| John | somewhere | 12345 | M | Science | | YES |
| John | somewhere | 12345 | M | English | | YES |
| Matt | somewhere | 123456 | M | | Teacher 2 | YES |
| Matt | somewhere | 23456 | M | Math | | YES |
| Matt | somewhere | 123456 | M | Science | | YES |
| Girl | somewhere | 5431 | F | | Teacher3 | YES |
| Girl | somewhere | 5431 | F | Physics | | YES |
| Girl | somewhere | 5431 | F | Math | | YES |
+------+-----------+-----------+-----+----------+-----------+--------+
select * from student_record;
+------------+------+-----------------+-----------+-----+----------+--------+
| id_student | name | address | telephone | sex | teacher | active |
+------------+------+-----------------+-----------+-----+----------+--------+
| 1 | John | Somewhere | 12345 | M | Teacher | 0 |
| 2 | Matt | Somewhere There | 12345222 | M | Teacher1 | 0 |
| 3 | Girl | Somewhere here | 3333 | F | Teacher2 | 0 |
+------------+------+-----------------+-----------+-----+----------+--------+
select * from subjects;
+------------+--------------+---------------------+
| id_subject | subject_name | subject_description |
+------------+--------------+---------------------+
| 1 | Math | Math |
| 2 | Science | Science |
| 3 | English | English |
| 4 | Physics | Physics |
+------------+--------------+---------------------+
select * from with_subjects;
+--------------------+--------------------+------------+
| id_student_subject | student_id_subject | student_id |
+--------------------+--------------------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 4 | 2 |
| 6 | 3 | 2 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 11 | 3 | 3 |
| 12 | 4 | 3 |
+--------------------+--------------------+------------+
how about
select a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",null as "Subject",a.teacher as "Teacher",a.active as "Active" from student_record as a
union a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",b.subject_name as "Subject",null as "Teacher",a.active as "Active" from (student_record as a inner join with_subjects as c on a.id_student = c.student_id) inner join subjects as b on c.student_id_subject = b.id_subject
Not tested it. It wotn be in the same order as your example, but should have all of the data there
I have 2 tables:
tb_sentence :
================================
|id|doc_id|sentence_id|sentence|
================================
| 1| 1 | 0 | AB |
| 2| 1 | 1 | CD |
| 3| 2 | 0 | EF |
| 4| 2 | 1 | GH |
| 5| 2 | 2 | IJ |
| 6| 2 | 3 | KL |
================================
First, I count the number of sentence in every document_id and save them in a variable $total_sentence.
So the value of $total_sentence variable is Array ( [0] => 2 [1] => 4 )
the second table is tb_stem :
============================
|id|stem|doc_id|sentence_id|
============================
|1 | B | 1 | 0 |
|2 | A | 1 | 1 |
|3 | C | 2 | 0 |
|4 | A | 2 | 1 |
|5 | E | 2 | 2 |
|6 | C | 2 | 3 |
|7 | D | 2 | 4 |
|8 | G | 2 | 5 |
|9 | A | 2 | 6 |
============================
Second, I need to group the datas of stem in every doc_id and then count the number of sentence_id that consist of the result before ($token). the concept is dividing the total number of documents by the number of documents containing the stem.
the code :
$query1 = mysql_query("SELECT DISTINCT(stem) AS unique FROM `tb_stem` group by stem,doc_id ");
while ($row = mysql_fetch_array($query1)) {
$token = $row['unique']; //the result $token must be : ABACDEG
}
$query2 = mysql_query("SELECT stem, COUNT( DISTINCT sentence_id ) AS ndw FROM `tb_stem` WHERE stem = '$token' GROUP BY stem, doc_id");
while ($row = mysql_fetch_array($query2)) {
$ndw = $row['ndw']; //the result must be : 1122111
}
$idf = log($total_sentence / $ndw)+1; //$total_sentence for doc_id = 1 must be divide $ndw with the doc_id = 2, etc
But The result is not separate between the different document like the table below :
============================
|id|word|doc_id| ndw |idf |
============================
|1 | A | | | |
|2 | B | | | |
|3 | C | | | |
|4 | D | | | |
|5 | E | | | |
|6 | G | | | |
============================
the result must be :
============================
|id|word|doc_id| ndw |idf |
============================
|1 | A | 1 | | |
|2 | B | 1 | | |
|3 | A | 2 | | |
|4 | C | 2 | | |
|5 | D | 2 | | |
|6 | E | 2 | | |
|7 | G | 2 | | |
============================
Help me please, Thank you :)
The formula of idf is idf = log(N/df) where N is the number of document and df is the number of documents in which a term (t) appears. Every sentence is considered as a document.
Here is example for idf calculation :
Document : Do you read poetry while flying. Many people find it relaxing to read on long flights
=================================================
| Term | Document1(D1)| D2| df | idf |
=================================================
| find | 0 | 1 | 1 |log(2/1)|
| fly | 1 | 1 | 2 |log(2/2)|
| long | 0 | 1 | 1 |log(2/1)|
| people | 0 | 1 | 1 |log(2/1)|
| poetry | 1 | 0 | 1 |log(2/1)|
| read | 1 | 1 | 2 |log(2/2)|
| relax | 0 | 1 | 1 |log(2/1)|
=================================================
This query will give you the table you're looking for:
SELECT t1.doc_id, t2.token as word, t2.token_freq as df,
log(t1.docs/t2.token_freq) as idf
FROM
(SELECT doc_id,count(sentence_id) as docs from tb_sentence group by doc_id) as t1,
(SELECT DISTINCT(stem) as token, doc_id, COUNT(sentence_id) as token_freq
FROM tb_stem GROUP BY doc_id, token) as t2
WHERE t1.doc_id = t2.doc_id
Note: Unique in your original query is a reserved word in MySQL and will give you errors.