用MVC架构时,怎么进行多表联合查询?求一比较优秀的解决办法
用MVC架构时,怎么进行多表联合查询?求一比较优秀的解决办法
回复讨论(解决方案)
任何框架都不可能预测你的需求
你只能按框架的约束组织你的数据
多表查询和MVC框架没有任何关系。
我给你说一些常用的方法吧,
1. 简单的,select 嵌套 select : select A.c1,A.c2,(select B.c3 from B where A.c4=B.c5) from A where c6=xxx
2. 简单的,select多表: select A.c1,A.c2,B.c3,B.c4 from A,B where A.c5=B.c6
3. 灵活的 join, left join, right join, outer join
4. union, union all
5. 上面两种或多种混合使用
其实难的sql查询还有很多情况,MVC框架不能够帮你进行多表查询的优化,所以优化了一条查询语句,可能就会让你的系统效率更高。
仅代表个人愚见,笑纳。
多表查询和MVC框架没有任何关系。
我给你说一些常用的方法吧,
1. 简单的,select 嵌套 select : select A.c1,A.c2,(select B.c3 from B where A.c4=B.c5) from A where c6=xxx
2. 简单的,select多表: select A.c1,A.c2,B.c3,B.c4 fr……
我只是想知道,在用MVC开发时,是怎么转向到原生态的SQL,是直接在控制里写很多JOIN 这些吗?
任何框架都不可能预测你的需求
你只能按框架的约束组织你的数据
大侠,可不可以举一两个例子。
如果你用的框架有规范,则按照它的规范来写
如果没有,则原生的 SQL 应该可以。多表查询也是如此。
如果你用的框架有规范,则按照它的规范来写
如果没有,则原生的 SQL 应该可以。多表查询也是如此。
框架有提供一些规范,我是用TP的来做的。之前没有用框架时,就是写了很多的JOIN语句,发现维护起来不容易,如果要修改,要重新去理解去读懂,有可能还会改错了,或范一些比较低级的错误,比如写单引号忘了写之类的。
见手册 6.23 关联模型
我有一个疑问,类似 销售订单表和客户表、用户表,这种表的关系,用面向对象要怎么设计呢
手册上不是有例子吗?
如果你用的框架有规范,则按照它的规范来写
如果没有,则原生的 SQL 应该可以。多表查询也是如此。
+++ 是的......
引用 3 楼 wangzhlin 的回复:多表查询和MVC框架没有任何关系。
我给你说一些常用的方法吧,
1. 简单的,select 嵌套 select : select A.c1,A.c2,(select B.c3 from B where A.c4=B.c5) from A where c6=xxx
2. 简单的,select多表: select A……
根据框架封装的db类了,查看手册或代码,或者你自己用原生的sql连接去写,数据库部分不用框架。
希望对你有帮助。
楼主可以封装成为视图 直接实例化试图就可 如果网站不是很大的话
多表联合查询跟mvc有什么关系?你还是没真正理解mvc是什么。
把你的联合查询封装在model里,由controller去调来获取数据就可以了。
楼上说封装成db的视图,也是一种方法。我发现很少有人在mysql上使用视图。不知为什么。其实视图可以解决很多问题。比如表结构更新后,只需要重新创建视图名称不变就可以了。而不用再去修改代码。可能是现在的新技术太多了,大家都不屑于使用这些老技术了。
谢谢各位的意见。
其实我想针对这种表联合查询的情况,做一个比较好的处理方案,为以后维护方便。最好是能结合类。我之前的系统是用原生态的SQL,发现修改时,容易写错是一个原因,也不好理解这个业务流程。
如果能把表与表的关系,用类来表现,这个方法不知怎么样。
多表查询和Mvc有什么关系
我的系统是用MVC架构的,而一个系统功能里,都会有多表查询的情况。
那么我想知道的是,各位在使用MVC架构开发时,遇到这种多表查询的情况,是直接用原生态SELECT,还是用视图或者存储过程
一般都是 原生态SELECT ,你可以去看看 mvc框架或者cms 的源码就知道了。
一般都是 原生态SELECT ,你可以去看看 mvc框架或者cms 的源码就知道了。
如果用原生态SELECT连接查询出来的数据,用什么对象接收啊?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The mouse is one of the most important computer peripherals. However, during use, the mouse wheel will inevitably fail or jump up and down, which greatly affects the user's operation. Is there any way to solve this problem? Let’s take a look at three ways to solve the problem of random jumping of the mouse wheel. Method 1: Check the mouse. Mouse produced by different brands have their own characteristics. Some mouse wheels have high sensitivity or low damping, which may cause the wheel to jump erratically. To determine if the problem is with the mouse itself, you can lightly touch the mouse wheel and watch the page move on the screen. If you find that your mouse scrolls too sensitively, you may want to consider buying a new mouse to fix the problem. Method 2: Check the mouse settings Improper mouse settings may cause the mouse wheel to jump randomly.

PyCharm is a powerful Python integrated development environment (IDE) that is widely used in the daily work of Python developers. However, sometimes we may encounter the problem that PyCharm cannot be opened normally, which may affect the progress of our work. This article will introduce some common problems and solutions when PyCharm cannot be opened, and provide specific code examples, hoping to help you solve these problems smoothly. Problem 1: PyCharm crashes or becomes unresponsive. Possible reasons: PyCh

Solution to Error 629 Broadband Connection With the rapid development of Internet technology, broadband has become an indispensable part of our daily lives. However, sometimes we may encounter some problems while using broadband, such as Error 629 Broadband Connection Error. This error usually causes us to be unable to access the Internet normally and brings a lot of inconvenience. In this article, we will share some methods to solve Error 629 Broadband Connection to help you solve this problem quickly. First, Error 629 broadband connection issues are usually caused by network configuration or driver issues

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

With the popularity of Python, PyQt5 has become one of the first choice tools for many people for rapid GUI development. However, installation problems are inevitable. The following are several common problems with PyQt5 installation and their solutions. The error message when installing PyQt5 is that the sip module cannot be found. This problem usually occurs when using pip to install PyQt5. The reason for this error is the lack of dependencies on the sip module, so you need to install the sip module manually first. Enter the following code on the command line: pipinsta

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

Washing machines play an important role in our daily lives. However, occasionally the buttons of the washing machine are found to be malfunctioning. This situation may bother us because it affects our normal washing operations. However, don’t worry, this article will introduce you to some methods to solve the problem of malfunctioning buttons on your washing machine. 1. Check the power connection - the plug is plugged in properly: Make sure the power plug of the washing machine is firmly inserted into the socket. -Whether the power switch is on: Check whether the power switch is on. 2. Clean the surface of the buttons - shut down: first turn off the washing machine and unplug the power plug to ensure safety. Washing Machine Button Failure Troubleshooting Warm Water Cleaning Use warm water and a neutral detergent to gently wipe the surface of the button. Button adjustment and restart adjustment button position: check whether the button is

Paper address: https://arxiv.org/abs/2307.09283 Code address: https://github.com/THU-MIG/RepViTRepViT performs well in the mobile ViT architecture and shows significant advantages. Next, we explore the contributions of this study. It is mentioned in the article that lightweight ViTs generally perform better than lightweight CNNs on visual tasks, mainly due to their multi-head self-attention module (MSHA) that allows the model to learn global representations. However, the architectural differences between lightweight ViTs and lightweight CNNs have not been fully studied. In this study, the authors integrated lightweight ViTs into the effective
