尊龙凯时人生就是博

thinkphpÖÐÔõôŲÓÃmysql×Ö¶Î

Ò»¡¢½¨ÉèÊý¾Ý¿â±íºÍÊý¾Ý

ÔÚ¾ÙÐÐÊý¾Ý¿â²Ù×÷֮ǰ£¬ÎÒÃÇÐèÒªÏȽ¨ÉèÊý¾Ý¿â¼°ÏìÓ¦µÄÊý¾Ý±í¡£ÎÒÃǼÙÉèÐèÒª¿ª·¢Ò»¸öѧÉúÖÎÀíϵͳ£¬ÆäÖÐÐèÒª½¨ÉèÒ»¸öÃûΪstudentµÄÊý¾Ý¿â±í£¬ÒԴ洢ѧÉúµÄ»ù±¾ÐÅÏ¢¡£±í°üÀ¨ÒÔÏÂ×ֶΣº

id£ºÖ÷¼ü£¬×ÔÔöÌí¡£

name£ºÑ§ÉúÐÕÃû£¬varcharÀàÐÍ£¬³¤¶ÈΪ20¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

age£ºÑ§ÉúÄêË꣬intÀàÐÍ¡£

sex£ºÑ§ÉúÐÔ±ð£¬varcharÀàÐÍ£¬³¤¶ÈΪ2¡£

t_score£ºCET-4¿¼ÊÔЧ¹û£¬intÀàÐÍ¡£

total_score£ºÑ§ÉúЧ¹û×ܺÍ£¬intÀàÐÍ¡£

ÎÒÃÇ¿ÉÒÔʹÓÃÈçϵÄSQLÓï¾ä½¨Éè¸ÃÊý¾Ý±í£º

CREATE TABLE 
student
 (
  
id
 int(11) NOT NULL AUTO_INCREMENT COMMENT '×ÔÔöID',
  
name
 varchar(20) NOT NULL DEFAULT '' COMMENT 'ѧÉúÐÕÃû',
  
age
 int(11) NOT NULL DEFAULT '0' COMMENT 'ѧÉúÄêËê',
  
sex
 varchar(2) NOT NULL DEFAULT '' COMMENT 'ѧÉúÐÔ±ð',
  
t_score
 int(11) NOT NULL DEFAULT '0' COMMENT 'CET-4¿¼ÊÔЧ¹û',
  
total_score
 int(11) NOT NULL DEFAULT '0' COMMENT 'ѧÉúЧ¹û×ܺÍ',
  PRIMARY KEY (
id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='ѧÉúÐÅÏ¢±í';

µÇ¼ºó¸´ÖÆ

ÎÒÃÇËæ»ú²åÈëһЩÊý¾Ý£¬ÓÃÓÚºóÐø²âÊÔ£º

INSERT INTO 
student
 (
name
, 
age
, 
sex
, 
t_score
, 
total_score
) VALUES ('ÕÅÈý', 20, 'ÄÐ', 550, 750), ('ÀîËÄ', 19, 'Å®', 530, 700), ('ÍõÎå', 22, 'ÄÐ', 610, 820), ('ÕÔÁù', 23, 'Å®', 590, 780), ('Ç®Æß', 21, 'ÄÐ', 500, 730);

µÇ¼ºó¸´ÖÆ

¶þ¡¢ÔÚThinkPHPÖÐŲÓÃMySQL×Ö¶Î

ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàÌṩµÄÒªÁ죬ÔÚThinkPHPÖжÔÊý¾Ý¿â¾ÙÐÐÔö¡¢É¾¡¢¸Ä¡¢²éµÈ²Ù×÷¡£ÔÚ¾ÙÐÐÅÌÎʲÙ×÷ʱ£¬ÎÒÃÇÐèҪŲÓÃMySQL×ֶΡ£ÒÔÏÂÁгöÁËһЩ³£ÓõÄŲÓ÷½·¨£º

1.ʹÓÃÊý×é·½·¨Ö±½ÓŲÓãº

ÎÒÃÇ¿ÉÒÔʹÓÃÊý×é·½·¨À´Ö±½ÓŲÓÃMySQL×ֶΡ£ÅÌÎÊstudent±íÖеÄid¡¢name¡¢ageµÈ×ֶΣ¬¿Éͨ¹ýÒÔÏ·½·¨ÊµÏÖ£º

$studentList = Db::name(‘student’)->field([‘id’, ‘name’, ‘age’])->select();

$field²ÎÊýΪ¿ÉÑ¡£¬²»´«Èë¸Ã²ÎÊýÌåÏÖÅÌÎÊËùÓÐ×ֶΡ£

2.ʹÓÃ×Ö·û´®·½·¨Å²Óãº

µÚ¶þÖÖ·½·¨ÎªÖ±½ÓʹÓÃ×Ö·û´®·½·¨Å²ÓÃMySQL×ֶΣ¬ÀýÈ磺

//ÅÌÎÊid£¬nameºÍage×Ö¶Î

$studentList = Db::name(‘student’)->field(‘id, name, age’)->select();

//ÅÌÎÊt_score£¬total_scoreºÍ×Ü·Ö×ֶΣ¨×Ü·ÖΪt_scoreºÍtotal_scoreÏà¼ÓµÄЧ¹û£©

$studentList = Db::name(‘student’)->field(‘t_score, total_score, (t_score+total_score) as score’)->select();

ÔÚʹÓÃ×Ö·û´®·½·¨Å²ÓÃMySQL×Ö¶Îʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýasΪij×Ö¶ÎÓÖÃû£¨alias£©¡£

3.ʹÓÃÄ£×Ó·½·¨Å²Óãº

ʹÓÃÄ£×Ó·½·¨Å²ÓÃʱ£¬ÎÒÃÇÐèÒªÏȽç˵ģ×ÓÀà¡£Ò»¸öÃûΪStudentµÄÄ£×ÓÀà¿ÉÒÔƾ֤student±íµÄ½á¹¹¾ÙÐнç˵

namespace app\common\model;

use think\Model;

class Student extends Model

{

//½ç˵±íÃûºÍÖ÷¼ü
protected $table = 'student';
protected $pk = 'id';

µÇ¼ºó¸´ÖÆ

}

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃStudentÄ£×ÓÀàÖ±½Ó²Ù×÷ÅÌÎÊ£º

//»ñÈ¡ËùÓÐѧÉúÁбíÐÅÏ¢

$studentList = Student::field([‘id’, ‘name’, ‘age’])->select();

//»ñȡѧÉúÐÕÃûºÍ×Ü·Ö

$studentList = Student::field(‘name, (t_score+total_score) as score’)->select();

ͨ¹ýÄ£×Ó·½·¨Å²ÓÃMySQL×ֶοÉÒÔʹÎÒÃǵĴúÂëÔ½·¢¾«Á·ÓÅÑÅ£¬²¢ÇÒÄܹ»ÓÐÓñÜÃâSQL×¢ÈëÎÊÌâ¡£

Èý¡¢ÔÚThinkPHPÖоÙÐÐMySQL×Ö¶ÎÓÖÃû²Ù×÷

ÔÚÖ´ÐÐSQLÅÌÎÊÓï¾äʱ£¬ÓÐʱÎÒÃÇÐèҪʹÓÃÁÐÓÖÃû×÷Ϊ·µ»ØЧ¹ûµÄ±êʶ¡£ÀýÈ磬ÎÒÃÇÒªÔÚÅÌÎÊЧ¹ûÖÐÏÔʾѧÉúÐÕÃûºÍ±È¸ÃÃûѧÉú·ÖÊý¸ßµÄÆäËûѧÉúÊýÄ¿£¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒ»¸öÌåÃû£¨alias£©£º

$studentList = Db::name('student')->field('name, (SELECT count(*) FROM student as b WHERE b.total_score>s.total_score) as rank')->alias('s')->select();

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëͨ¹ý×ÓÅÌÎʺÍaliasÒªÁ죬¿ÉÒÔ»ñµÃÈËÃûºÍÅÅÃû£¬Æä¶ÔÓ¦µÄSQLÓï¾äΪ£º

SELECT name, (SELECT count(*) FROM student as b WHERE b.total_score>s.total_score) as rank FROM student s

µÇ¼ºó¸´ÖÆ

ÔÚThinkPHPÖУ¬Í¨¹ýʹÓÃaliasÒªÁ죬ÎÒÃÇ¿ÉÒÔΪMySQL×Ö¶ÎÉèÖÃÖ¸¶¨µÄÓÖÃû£¬ÒÔµÖ´ï¸ü¸ßЧµÄÅÌÎʲÙ×÷¡£

ÒÔÉϾÍÊÇthinkphpÖÐÔõôŲÓÃmysql×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ