Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Table relations image is in this link http://www.serofero.net/mvb-table-relation.png
I am using php as server side programming language and MySQL as database.
Problem Description
User adds a new venue. One venue may have multiple beverages, events, features so on and so forth. Now , I want such a query or magic so that I could collect all the beverages, events, features, foods, styles, types, event_options and space_requirements related to each venue along with its venue_id, name, description, capacity, min_rate, max_rate, location (from locations table). Also I need to offset and limit the result so that I could implement pagination in backend. But the challange is that the limit should limit the number of venues not its beverages, foods, styles etc.
I am also wondering to collect the result in php array as below:
$result = array(
0=> array(
"name" => "Venue A",
"description" => "Venue A description",
"capacity" => "Venue A capacity",
"location" => "Venue A location",
"beverages" => array('beverage1','beverage23','beverage7',...),
"events" => array('event8','event17','event19','event4',...),
"features" => array('features1',...),
"foods" => array(),
"styles" => array(),
"types" => array('type7', 'type14', 'type23',...),
"event_options" => array(),
"space_requirements" => array()
)
,
1=> array(
"name" => "Venue B",
"description" => "Venue B description",
"capacity" => "Venue B capacity",
"location" => "Venue B location",
"beverages" => array('beverage1'),
"events" => array('event2','event7','event9','event4',...),
"features" => array(),
"foods" => array(),
"styles" => array('style1', 'style2',...),
"types" => array('type47', 'type4', 'type3',...),
"event_options" => array(),
"space_requirements" => array()
)
);
Today is 5th day I am trying to figure out the solution but I have been failed all the time. Below is the snippet of MySQL Query that I could write till now.
SELECT v.name, e.event, t.type, s.style
FROM venues v
LEFT JOIN venue_events ve ON v.venue_id = ve.venue_id
LEFT JOIN events e ON e.event_id = ve.event_id
LEFT JOIN venue_types vt ON v.venue_id = vt.venue_id
LEFT JOIN types t ON t.type_id = vt.type_id
LEFT JOIN venue_styles vs ON v.venue_id = vs.venue_id
LEFT JOIN styles s ON s.style_id = vs.style_id
WHERE v.venue_id IN (SELECT venue_id FROM venues) LIMIT 0,5
/* I want to limit the number of "venues" but the LIMIT 0,5 limits the number of 'events', 'types' , 'styles' the "venue" have. And this is the main problem.
I have also tried :
WHERE v.venue_id IN (SELECT venue_id FROM venues LIMIT 0,5) but it raises the MySQL error.
*/
But I dont know what to do next to get the result as I mentioned above.
Please help me.
Thankyou.
SELECT DISTINCT ven.venue_id, ven.name, e.event_id, e.event, t.type_id, t.type, s.style_id, s.style
FROM (SELECT * FROM venues v LIMIT 0,5) ven /*This line does magic for me*/
LEFT JOIN venue_events ve ON ven.venue_id = ve.venue_id
LEFT JOIN events e ON e.event_id = ve.event_id
LEFT JOIN venue_types vt ON ven.venue_id = vt.venue_id
LEFT JOIN types t ON t.type_id = vt.type_id
LEFT JOIN venue_styles vs ON ven.venue_id = vs.venue_id
LEFT JOIN styles s ON s.style_id = vs.style_id
Related
I have the following table structure:
So a product may have multiple product_type's.
When I do my Join in MySQL I get repeated product records for each product_type. So for example if I have product called Actifoam and it has 2 records in the products_types table then the result of the query will include Actifoam twice (each one with a different product_type.
Here is my query:
SELECT DISTINCT product.*, product_type.name as product_type_name
FROM products_types
JOIN product_type ON product_type.id = products_types.product_type_id
JOIN product ON product.id = products_types.product_id
Here is what the result of the query shows:
[
0 => [
'id' => 'recccAQHxsb4OEsX6'
'name' => 'Actifoam'
'product_type_name' => 'Silver Dressing'
]
1 => [
'id' => 'recccAQHxsb4OEsX6'
'name' => 'Actifoam'
'product_type_name' => 'Foam'
]
]
I don't want the product records to appear multiple times like this, I would like everything to appear on a single row if there are multiple product_types.
Anyone have an idea how I can achieve this?
DISTINCT will return unique rows only if 2 rows have exact same corresponding values for all columns in SELECT statement. In your case you need GROUP BY and GROUP_CONCAT.
GROUP_CONCAT will return product_type_name separated by comma.
SELECT product.*, GROUP_CONCAT(product_type.name) as product_type_name
FROM products_types
JOIN product_type ON product_type.id = products_types.product_type_id
JOIN product ON product.id = products_types.product_id
GROUP BY product.id
In doctrine, I want to fetch multiple entities from a native query. But I can't workout how to do it.
I have 3 entities: WBSOProject, Employee and WBSOBooking. Employees can log their work hours through a WBSOBooking entity which couples the Employee with a WBSOProject and it has an extra field for the logged hours.
To generate an overview of the worked hours per employee per project I've written the following code:
$query = "
select
project.id as project_id,
project.name,
employee.id as employee_id,
employee.bsn,
employee.name,
sec_to_time(sum(time_to_sec(wbsoBooking.total))) as balance
from wbso_projects project
inner join wbso_bookings as wbsoBooking on wbsoBooking.project_id = project.id
inner join employees as employee on employee.id = wbsoBooking.employee_id
group by project.year, project.name, employee.id, employee.name
";
$rsm = new ResultSetMapping();
$rsm
->addEntityResult("Kloktijden\\Model\\Entities\\WBSOProject", "project")
->addFieldResult("project", "project_id", "id")
->addFieldResult("project", "name", "name")
->addEntityResult("Kloktijden\\Model\\Entities\\Employee", "employee")
->addFieldResult("employee", "employee_id", "id")
->addFieldResult("employee", "bsn", "bsn")
->addFieldResult("employee", "name", "name")
->addScalarResult("balance", "balance");
$result = Model::getEntityManager()
->createNativeQuery($query, $rsm)
->getResult();
My desired result would be an array like this:
array(
[0] => array(
[0] => Kloktijden\\Model\\Entities\\WBSOProject,
[1] => Kloktijden\\Model\\Entities\\Employee,
"balance" => "00:00"
)
)
But somehow the Employee entity gets ignored, so my result looks like this:
array(
[0] => array(
[0] => Kloktijden\\Model\\Entities\\WBSOProject,
"balance" => "00:00"
)
)
So, my question is: why doesn't my resultset contain the Employee entity? And how can I fix this?
Trying to join 3 tables: leagues, leagues_teams, and teams.
With the query:
$teams = $database->select(
'teams',
[
'[>]leagues_teams' =>
[
'fifa_id' => 'fifa_team_id'
],
'[>]leagues' =>
[
'fifa_league_id' => 'fifa_id'
]
], [
'teams.fifa_id',
'teams.name',
'teams.rating'
], [
'ORDER' => 'teams.name ASC'
]
);
Which results in the following query:
SELECT "teams"."fifa_id","teams"."name","teams"."rating"
FROM "teams"
LEFT JOIN "leagues_teams" ON "teams"."fifa_id" = "leagues_teams"."fifa_team_id"
LEFT JOIN "leagues" ON "teams"."fifa_league_id" = "leagues"."fifa_id"
ORDER BY "teams"."name" ASC
When adding the second LEFT JOIN, it should join with leagues_teams.fifa_league_id=leagues.fifa_id and not teams.fifa_league_id
How would I go about this join?
In case someone was still having this problem, from MEDOO documentation:
You can refer the previous joined table by adding the table name before the column.
Example:
"[>]album" => ["account.user_id" => "user_id"],
MEDOO - SELECT API
I'm a new in Zend.
In my model Default_Model_ApplicationMapper, I wanna use a db-table Application_Model_DbTable_Application to run a query like that
"Select * from application as a, category as c, user as u where a.user_id = u.id and a.category_id = c.id";
I tried this:
$table = new Application_Model_DbTable_Application();
$select = $table->select()->from(array('a' => 'application'))
->from(array('c' => 'category'))
->from(array('u' => 'user'))
->where('a.user_id = u.id and a.category_id = c.id');
Of course this thing does not work.
Can you help me with this?
Instead of using from(array('a' => 'table')) three times, try using from(array('a' => 'table1', 'b' => 'table2', 'c' => 'table3')).
I'm trying to get nested arrays for my Cakephp custom query below:
$this->query("
SELECT *
FROM group_buys GroupBuy
LEFT JOIN products Product
ON Product.id = GroupBuy.product_id
LEFT JOIN group_buy_users GroupBuysUser
ON GroupBuysUser.group_buy_id = GroupBuy.id
LEFT JOIN group_buy_images GroupBuyImage
ON GroupBuyImage.group_buy_id = GroupBuy.id
LEFT JOIN product_details ProductDetail
ON ProductDetail.product_id = Product.id
LEFT JOIN specifications Specification
ON Specification.id = ProductDetail.specification_id
LEFT JOIN specification_categories SpecificationCategory
ON SpecificationCategory.id = Specification.specification_category_id
WHERE GroupBuy.id = {$id}
");
Problem with this is that it comes up with redundant data obviously with GroupBuy table row values repeating which I don't want.
Is there a way we can have nested arrays if LEFT JOINED table has more rows than the former table with Cake's custom query?
I know this can be done with find recursive = 2 but would like to achieve this with custom query.
Have you tried using containable?
$this->GroupBuy->Behaviors->attach('Containable');
$this->GroupBuy->find('all', array(
'conditions' => array('GroupBuy.id' => $id),
'contain' => array(
'Product' => array(
'ProductDetail' => array(
'Specification' => array(
'SpecificationCategory'
)
)
),
'GroupBuysUser',
'GroupBuyImage'
),
));