我有一个地址表和一个居民表以一对多方式加入,以便查询
[{
"id": 806,
"code": "AYC8rk",
"postcode": "GL141PQ",
"address": "THE COTTAGE, ADSETT COURT, LONDON ",
"resident": [{
"id": 5441,
"firstname": "THOMAS",
"surname": "MARSHALL"
}, {
"id": 5442,
"firstname": "ANNA",
"surname": "RAYMOND"
}, {
"id": 6188,
"firstname": "WILL",
"surname": "MARSHALL"
}]
}]
会返回类似这样的内容
House::with('resident')
->where('postcode', 'GL141PQ', 'and')
->where('surname', 'MARSHALL')
->get();
如果该邮政编码中有很多属性,我需要扩展查询以在Residents表中包含姓氏,我很难过。下面显然不起作用所以我假设我需要以某种方式迭代连接表。
...list of modules...
seed = 342
np.random.seed(seed)
train_data = pd.read_csv("./train.csv")
test_data = pd.read_csv("./test.csv")
test_id = test_data.ID
train_data = train_data.drop(["ID"],axis=1)
test_data = test_data.drop(["ID"],axis=1)
array = train_data.values
# Split-out validation dataset
X = array[:,1:368].astype(float)
Y = array[:,369].astype(int)
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=validation_size, random_state=seed)
watchlist = [(X_train, Y_train), (X_validation, Y_validation)]
ratio = float(np.sum(Y_train == 1)) / np.sum(Y_train==0)
model = xgb.XGBClassifier(
base_score=0.5,
colsample_bylevel=1,
colsample_bytree=1,
gamma=0,
learning_rate=0.5,
max_delta_step=0,
max_depth=3,
min_child_weight=1,
missing=9999999999,
n_estimators=25,
nthread=-1,
objective='binary:logistic',
reg_alpha=0,
reg_lambda=1,
scale_pos_weight=1,
seed=7,
silent=True,
subsample=1
)
model.fit(X_train, Y_train, eval_metric="auc", eval_set=watchlist)
## # Submission
probs = model.predict_proba(test_data)
...
非常感谢任何帮助!
答案 0 :(得分:0)
使用像这样的Laravel with()
只是懒得将居民加载到你的House模型上,它没有加入它。
你可能会做这样的事情(未经测试):
House::with(['resident' => function($query) use ($surname) {
$query->where('surname', $surname);
}])
->where('postcode', 'GL141PQ')
->get();
编辑:
然后你必须自己添加联接。如上所述with()是延迟加载,而不是加入。
House::with('resident') // lazy load all residents after query is done, you don't want to filter here
->leftJoin('resident', 'house.id', '=', 'resident.house_id')
->where('resident.surname', $surname)
->where('house.postcode', 'GL141PQ')
->groupBy('house.id')
->get();
或
House::with('resident') // lazy load all residents after query is done, you don't want to filter here
->leftJoin('resident', function($join) use ($surname) {
$join->on('house.id', '=', 'resident.house_id')
->where('resident.surname', $surname)
})
->where('house.postcode', 'GL141PQ')
->groupBy('house.id')
->get();
这些将为您提供所需的结果。
请注意,Laravel的->count()
和->groupBy(x)
彼此不太喜欢。