finding products that are bought together Laravel eloquent - php

I am using Laravel and eloquent If I have the following two tables
Sales
----
|id|
|--|
|1 |
|2 |
|3 |
|4 |
----
Sales_data
----
|sale_id|Product_id|
|------------|
| 1 | 30 |
| 1 | 24 |
| 1 | 18 |
| 2 | 18 |
| 2 | 30 |
| 3 | 24 |
| 4 | 18 |
| 4 | 24 |
--------------
I would like to retrieve the following info:
| Product_id | bought_with | times_bought_together |
|--------------|-------------|-----------------------|
| 30 | 18 | 2 |
| 30 | 24 | 1 |
| 24 | 18 | 1 |
------------------------------------------------------
This is the relation in Sales model, to the sale_data
public function data()
{
return $this->hasMany(\App\Models\SaleData::class, 'sale_id');
}
Thank you verry much

You can use DB::select() so you can put RAW SQL statements in it.
In this case its easyer to just use RAW SQL
DB::select("SELECT c.product_id, c.bought_with, count(*) as times_bought_together
FROM (
SELECT a.product_id as product_id, b.product_id as bought_with
FROM sales_data a
INNER join sales_data b
ON a.sale_id = b.sale_id AND a.product_id != b.product_id) c
GROUP BY c.product_id, c.bought_with
ORDER BY times_bought_together DESC LIMIT 50")
GoodLuck if you have the same problem!

Related

How to manage SELECT data from 3 different table's using with JOIN?

I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;

Mysql select distinct id's and select first row only

This is my table. I want to select distinct 'bill_no' with only first related row.
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 2 | 12 | Spiker gel | 10 | 300 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 4 | 13 | test prod | 1 | 145 |
| 5 | 14 | wokk | 3 | 55 |
| 6 | 14 | Kamer hyp | 11 | 46 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
I want data to be displayed like the below table having only distinct "bill_no" -
Output-
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 5 | 14 | wokk | 3 | 55 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
Use group by
select * from youtable group by bill_no
select t1.*
from your_table t1
join
(
select min(id) as id
from your_table
group by bill_no
) t2 on t1.id = t2.id
You can use GROUP BY clause.GROUP BY clause always return first row with related group by cloumn name.
SELECT * FROM `table_1` group by `bill_no`

fetch data from multiple tables

I have 4 table for my Ads Portal.
Firstly main table name is ads
+-------------+
| Field +
+-------------+
| id +
| ads_title +
+-------------+
example data for ads table;
+----+-----------------+
| id | ads_title |
+----+-----------------+
| 20 | 2006 CITROEN C4 |
| 27 | EMEKLI OGRETMEN |
| 28 | Harika 10 n |
| 34 | HATASIZ BOYASIZ |
| 49 | BAYANDAN 2009 |
+----+-----------------+
for ads specifications stored in a table. table name is ads_spc
+---------+
| Field |
+---------+
| id |
| key_name|
+---------+
data example for ads_spc table;
+----+-------------+
| id | key_name |
+----+-------------+
| 1 | Date |
| 2 | Km |
| 3 | Colr |
| 4 | Engine |
| 5 | Pw. Engine |
| 6 | Oil |
| 7 | Speed |
| 8 | Boody Type |
| 9 | Traction |
| 10 | Warranty |
+----+-------------+
Lastly stored specification values for ads_spc by ads_id. table name is ads_values
+------------+
| Field |
+------------+
| id |
| ads_id |
| spc_id |
| value |
+------------+
for example this table data
+----+--------+--------+-------+
| id | ads_id | spc_id | value |
+----+--------+--------+-------+
| 25 | 49 | 9 | 2119 |
| 26 | 49 | 10 | 2131 |
| 27 | 34 | 1 | 2010 |
| 28 | 34 | 2 | 8900 |
| 29 | 34 | 3 | 4 |
| 30 | 34 | 4 | 22 |
| 31 | 34 | 5 | 40 |
| 32 | 34 | 6 | 60 |
| 33 | 34 | 7 | 65 |
| 34 | 34 | 8 | 66 |
+----+--------+--------+-------+
I want to using these tables list ads (table: ads) with specifications (table:ads_spc) and ads values (table:ads_values) in a data row.
Union to;
ADS + ADS_SPC + ADS_VALUES (All values searchable)
I'm using this case:
SELECT SQL_CALC_FOUND_ROWS
*
FROM ads AS a
LEFT JOIN ads_spc AS aspc
LEFT JOIN ads_values AS aval ON aval.ads_id=a.id AND aval.spc_id=aspc.spc_id
How can I union to one row?
Sorry for my english.
Why union there is no sense for the union you should join these tables
SELECT v.id, a.ads_title,s.key_name,v.value FROM `ads` a
INNER JOIN `ads_values` v ON (a.id =v.ads_id)
INNER JOIN `ads_spc` s ON(v.spc_id =s.id) WHERE s.key_name='Color'

Display 2 filtered columns from 2 tables in PHP

I have 2 tables - users and users_audit, need to display results from 2 separate queries into 2 columns with relation to each other. I already figured out both queries.
query for users: SELECT rowid,data FROM users WHERE columnid = 1 GROUP BY rowid;
+-------+------------+
| rowid | data |
+-------+------------+
| 1 | John |
| 2 | Abi |
| 3 | Tony |
| 4 | Gregg |
| 5 | Jon |
| 6 | Victor |
| 7 | Daniel |
query for users_audit: SELECT date_created FROM users_audit WHERE rowid = 6 ORDER BY date_created DESC, date_created DESC LIMIT 1;
Purpose of the query is to display latest date_created for particular rowid.
+---------------------+
| date_created |
+---------------------+
| 2012-07-04 09:20:12 |
+---------------------+
Table users_audit looks like this:
SELECT * FROM users_audit;
+-------+---------+--------------+-------------+---------------------+
| id | rowid | before_value | after_value | date_created |
+-------+---------+--------------+-------------+---------------------+
| 1 | 6 | 3 | 5 | 2012-06-29 15:48:11 |
| 2 | 5 | Out (0) | 2 | 2012-07-04 09:20:10 |
| 3 | 6 | 5 | 4 | 2012-07-04 09:20:12 |
| 4 | 7 | 3 | 6 | 2012-07-04 09:20:14 |
| 5 | 3 | 2 | 3 | 2012-07-04 09:20:16 |
| 6 | 15 | 6 | 5 | 2012-07-04 09:20:22 |
I need to display 2 columns - data from users and date_created from users_audit for each rowid. It means query for users_audit must be run for each rowid (in the loop?).
Expected result displayed in php is:
+------------+----------------------+
| data | date_created |
+------------+----------------------+
| John | (latest date) |
| Abi | (latest date) |
| Tony | (latest date) |
| Gregg | (latest date) |
| Jon | (latest date) |
| Victor | 2012-07-04 09:20:12 |
| Daniel | (latest date) |
How can this be achieved this in php?
Why cant you just create a view and use a join to create the table you want?
SELECT users.data, users_audit.date_created FROM users
INNER JOIN users_audit ON users.rowid = users_audit.rowid
Or am I missing something here?
EDIT
SELECT users.data, users_audit.date_created FROM users
INNER JOIN users_audit ON users.rowid = users_audit.rowid
WHERE users_audit.date_created = (SELECT MAX(date_created) from users_audit
GROUP BY rowid)
I believe this should work..? Let me know.

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories