fetch data from multiple tables - php

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'

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;

Laravel 5 - Retrieve the first 4 records for each category in database

I have a database table of gallery images which are categorised by the following:
'corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue'
Each image has one of these categories assigned to it.
I'm building a main gallery page in which I want to display the latest 4 images from each of these categories in the database.
Could someone assist as to how I can go about building the query?
The query starts as follows:
Bugz\GalleryImage::
Table Structure
Schema::create('gallery_images', function (Blueprint $table) {
//set the table engine:
$table->engine = 'InnoDb';
//define an auto-incrementing primary key:
$table->increments('id');
//define the general fields:
$table->enum('gallery', array('corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue'))->default('corporate');
$table->string('title');
$table->string('content')->nullable()->default(null);
//define the audit fields:
$table->timestamps();
$table->softDeletes();
});
I don't have enough experience yet with Eloquent to write more complex queries.
Thank you.
Here's one way...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(image_id INT NOT NULL ATO_INCREMENT PRIMARY KEY
, category ENUM('corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue') NOT NULL
);
INSERT INTO my_table (category) VALUES
('corporate'),
('food'),
('park'),
('parties'),
('rides'),
('schools'),
('venue'),
('rides'),
('schools'),
('venue'),
('food'),
('park'),
('parties'),
('rides'),
('corporate'),
('food'),
('park'),
('food'),
('park'),
('parties'),
('rides'),
('food'),
('park'),
('food'),
('corporate'),
('rides'),
('corporate'),
('parties'),
('rides'),
('corporate'),
('food'),
('schools'),
('venue'),
('venue'),
('food'),
('park'),
('parties')
;
Intermediate result...
SELECT x.*
, COUNT(y.image_id) temp_ranks_for_y
FROM my_table x
JOIN my_table y
ON y.category = x.category
AND y.image_id >= x.image_id
GROUP
BY x.image_id;
+----------+-----------+-------------------+
| image_id | category | temp_ranks_for_y |
+----------+-----------+-------------------+
| 1 | corporate | 5 |
| 2 | food | 8 |
| 3 | park | 6 |
| 4 | parties | 5 |
| 5 | rides | 6 |
| 6 | schools | 3 |
| 7 | venue | 4 |
| 8 | rides | 5 |
| 9 | schools | 2 |
| 10 | venue | 3 |
| 11 | food | 7 |
| 12 | park | 5 |
| 13 | parties | 4 |
| 14 | rides | 4 |
| 15 | corporate | 4 |
| 16 | food | 6 |
| 17 | park | 4 |
| 18 | food | 5 |
| 19 | park | 3 |
| 20 | parties | 3 |
| 21 | rides | 3 |
| 22 | food | 4 |
| 23 | park | 2 |
| 24 | food | 3 |
| 25 | corporate | 3 |
| 26 | rides | 2 |
| 27 | corporate | 2 |
| 28 | parties | 2 |
| 29 | rides | 1 |
| 30 | corporate | 1 |
| 31 | food | 2 |
| 32 | schools | 1 |
| 33 | venue | 2 |
| 34 | venue | 1 |
| 35 | food | 1 |
| 36 | park | 1 |
| 37 | parties | 1 |
+----------+-----------+-------------------+
So...
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.category = x.category
AND y.image_id >= x.image_id
GROUP
BY x.image_id
HAVING COUNT(y.image_id) <=4
ORDER
BY category
, image_id DESC;
+----------+-----------+
| image_id | category |
+----------+-----------+
| 30 | corporate |
| 27 | corporate |
| 25 | corporate |
| 15 | corporate |
| 35 | food |
| 31 | food |
| 24 | food |
| 22 | food |
| 36 | park |
| 23 | park |
| 19 | park |
| 17 | park |
| 37 | parties |
| 28 | parties |
| 20 | parties |
| 13 | parties |
| 29 | rides |
| 26 | rides |
| 21 | rides |
| 14 | rides |
| 32 | schools |
| 9 | schools |
| 6 | schools |
| 34 | venue |
| 33 | venue |
| 10 | venue |
| 7 | venue |
+----------+-----------+
27 rows in set (0.00 sec)
This code will retrives 4 images of every category.
if you want to retrive latest one then just add sort by cluse in outer query.
DB::table('gallery_images AS GI')
->where(function($query)
{
DB::table('tbl_dept_master AS GI2')
->select(DB::raw(count(1)))
->from('gallery_images As GI2')
->where('GI2.gallery','=','GI.gallery')
->where('GI2.title','>=','GI.title');
})
->get();
If need more help comment.
Hope this will sovle your problem.

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`

SELECT records WHERE rows have difference on a specific column

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;

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