我正在尝试编写一个查询,它将比较N行数的值,并仅返回具有最大值的行.例如,如果我只想返回一个包含非重复行的表,而只返回具有最新日期的行 -
key | name | value | date
1 | frank | 100 | 1/1/2013
2 | peter | 200 | 2/1/2013
3 | jonny | 300 | 3/1/2013
4 | jonny | 300 | 4/1/2013
Run Code Online (Sandbox Code Playgroud)
和查询:
SELECT key, name, value, MAX(date)
FROM myTable
WHERE key IN (1,2,3,4)
Run Code Online (Sandbox Code Playgroud)
我期待着回归
key | name | value | date
1 | frank | 100 | 1/1/2013
2 | peter | 200 | 2/1/2013
4 | jonny | 300 | 4/1/2013
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用GROUP BY,我想我错过了一些基本的尝试.
我可能正在接近这个......
我试图在内部跟踪对象状态并使用它来调用修改后的方法:
createObject = function() {
this.a = 1
this.method1 = function() {
if (this.a == 1 ) {
//do stuff
this.a = 0
}
}
var x = new createObject()
Run Code Online (Sandbox Code Playgroud)
不幸的是,州没有被内部跟踪.如果我改变另一个对象的属性,它可以完美地工作:
otherObj = { a:1 }
createObject = function() {
this.method1 = function() {
if (this.a == 1 ) {
//do stuff
otherObject.a = 0
}
}
var x = new createObject()
Run Code Online (Sandbox Code Playgroud)
这是接近这个的正确方法吗?
我想弄清楚如何在水晶报告中按组显示不同的记录。我写的视图返回如下内容:
Field 1 | Field 2 | Field 3
----------------------------------
10 | 111 | Record Info 1
10 | 111 | Record Info 1
10 | 222 | Record Info 2
20 | 111 | Record Info 1
20 | 222 | Record Info 2
Run Code Online (Sandbox Code Playgroud)
报告组基于第一个字段,我希望每个组有不同的字段 2 和 3:
Field 1 | Field 2 | Field 3
----------------------------------
10 | 111 | Record Info 1
10 | 222 | Record Info 2
20 | 111 | Record Info 1
20 …Run Code Online (Sandbox Code Playgroud) 这看起来非常简单,但我坚持使用一个简单的插入语句。见下文:
begin work;
CREATE TEMPORARY TABLE IF NOT EXISTS insert_table AS
(
select
r.resource_id
,fr.file_repos_id
,mv.VALUE
from
resources r
join versions v on v.RESOURCE_ID = r.resource_id
join metadata_values mv on mv.resource_id = r.resource_id
join file_repository fr on fr.file_repos_id = v.foreign_id
where
v.version_status = 'C'
and r.RESOURCE_TYPE = 4
and fr.file_title in ('suburbs')
);
insert
into
metadata_values (elem_id,value,resource_type,resource_id,foreign_id,mtvr_id,mett_id)
values
(62,'test',4,insert_table.resource_id,insert_table.file_repos_id,80,4);
rollback work;
Run Code Online (Sandbox Code Playgroud)
在临时表行中fr.file_title in ('suburbs'),实际列表是从其他地方动态拉取的(这是出于演示目的)。我收到以下错误消息:
错误代码:1054。“字段列表”中的未知列“insert_table.resource_id”
现在,我可以在临时表上运行一个选择,它返回正常,只是在更新语句中失败。我正在从 MySQL 工作台运行它。完全不知道这里发生了什么。
我正在尝试使用AJAX请求; 具体来说,我不想在控制台中记录每个更改,以便知道我正在这样做.代码如下:
function loadAJAX() {
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = readyStateSwitch(ajaxRequest.readyState);
function readyStateSwitch(x) {
switch(x) {
case 0: ajax0(); break;
case 1: ajax1(); break;
case 2: ajax2(); break;
case 3: ajax3(); break;
case 4: ajax4(); break;
default: console.log("no arg"); break;
}
}
function ajax0() {
console.log("0")
}
function ajax1() {
console.log("1")
}
....
ajaxRequest.open("GET", "index.php", true);
ajaxRequest.send();
}
Run Code Online (Sandbox Code Playgroud)
我希望在我的控制台中打印出0-4,但它只返回0(未初始化).有任何想法吗?
因此,在初始提交之前启动项目而不是正确设置源代码控制的快乐传统中,我已经破坏了我的Django Test Runner.似乎PATH在某处搞砸了,所以这可能不是Django特有的问题.
我可以成功运行django dev服务器并且所有项目功能都可以运行.我在该项目中可能有70个测试曾经工作过,直到某些时候我引入了一些使它们没有的东西.
错误:
======================================================================
ERROR: app.group.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
yield
File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 577, in run
testMethod()
File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: app.group.tests
Traceback (most recent call last):
File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
ImportError: No module named 'app.group'
Run Code Online (Sandbox Code Playgroud)
检查标准:
(venv) Jamies-MacBook-Pro:app Jamie$ python3
Python 3.4.1 (default, Jun …Run Code Online (Sandbox Code Playgroud) 我想知道用django模型形式创建模型实例的正确方法是什么时候它包含一个必需的FK?我认为它可能与excludeclass属性有关,但在视图中我尝试在保存之前覆盖它.
模型:
class Profile(models.Model):
client = models.OneToOneField('auth.User')
first_name = models.TextField(blank=True,)
...
Run Code Online (Sandbox Code Playgroud)
形成:
class ProfileForm(floppyforms.ModelForm):
class Meta:
exclude = ('client',)
model = Profile
widgets = {
'first_name': floppyforms.TextInput(attrs={'placeholder': 'First Name'}),
...
Run Code Online (Sandbox Code Playgroud)
视图:
def post(self, request):
form = ProfileForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.client = User.objects.create(username=request.POST['email'],)
form.save()
return redirect('/success')
return redirect('/error')
Run Code Online (Sandbox Code Playgroud)
错误:
django.db.models.fields.related.RelatedObjectDoesNotExist: Profile has no client.
Run Code Online (Sandbox Code Playgroud)
干杯
django ×2
javascript ×2
python ×2
sql ×2
ajax ×1
django-forms ×1
django-views ×1
mysql ×1
python-3.x ×1
sql-insert ×1
sql-server ×1
t-sql ×1
temporary ×1
unit-testing ×1