Mysql 选择主键 = x 的记录

发布于 2024-10-13 19:16:28 字数 921 浏览 3 评论 0原文

我的 mysql 表中有一个主键,它由三列组成。

CREATE TABLE IF NOT EXISTS `bb_bulletin` (
  `OfficeCode` int(5) NOT NULL,
  `IssuerId` int(11) NOT NULL,
  `BulletinDtm` datetime NOT NULL,
  `CategoryCode` varchar(4) NOT NULL,
  `Title` varchar(255) NOT NULL,
  `Content` text NOT NULL,
  PRIMARY KEY (`OfficeCode`,`IssuerId`,`BulletinDtm`),
  UNIQUE KEY `U_IssuerId` (`IssuerId`,`OfficeCode`,`BulletinDtm`),
  UNIQUE KEY `U_CategoryCode` (`CategoryCode`,`OfficeCode`,`IssuerId`,`BulletinDtm`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

是否有一种速记方法可以为给定的主键值选择记录。

我试过。

SELECT * FROM `bb_bulletin` WHERE PRIMARY = '20001-1-2011-01-07 14:04:40'

代替繁琐的方法,

SELECT * From bb_bulletin WHERE OfficeCode = 20001 AND IssuerId = 1 AND BulletinDtm = 2011-01-07 14:04:40

处理表中的 php 和复合键时的标准是什么。 注意:我不想将自动增量键添加到我的表中来解决这个问题。如果不可能,那么我将只在我的网址中传递三个约束。

I have a primary key in my mysql table which is comprised of three columns.

CREATE TABLE IF NOT EXISTS `bb_bulletin` (
  `OfficeCode` int(5) NOT NULL,
  `IssuerId` int(11) NOT NULL,
  `BulletinDtm` datetime NOT NULL,
  `CategoryCode` varchar(4) NOT NULL,
  `Title` varchar(255) NOT NULL,
  `Content` text NOT NULL,
  PRIMARY KEY (`OfficeCode`,`IssuerId`,`BulletinDtm`),
  UNIQUE KEY `U_IssuerId` (`IssuerId`,`OfficeCode`,`BulletinDtm`),
  UNIQUE KEY `U_CategoryCode` (`CategoryCode`,`OfficeCode`,`IssuerId`,`BulletinDtm`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Is there a shorthand method to select a record for a given value of the primary key.

I tried.

SELECT * FROM `bb_bulletin` WHERE PRIMARY = '20001-1-2011-01-07 14:04:40'

Instead of the long hand method of doing,

SELECT * From bb_bulletin WHERE OfficeCode = 20001 AND IssuerId = 1 AND BulletinDtm = 2011-01-07 14:04:40

What is the standard when dealing php and composite keys in your table.
Note: I don't want to add autoincrementing keys to my tables in order to solve this. If it is not possible then I will just pass the three constraints in my url.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨笨の傻瓜 2024-10-20 19:16:28

我看到你的问题有两个部分。第一部分是关于引用复合值。我不确定 MySQL 是否支持这一点,但这将是执行此操作的 SQL 标准方法:

SELECT * FROM bb_bulletin WHERE (OfficeCode, IssuerId, BulletinDtm) = (20001, 1, '2011-01-07 14:04:40');

另一部分是使用缩写语法引用主键列。我不知道有这样的可能性。

I see two parts to your question. The first part is about referencing a composite value. I'm not sure whether MySQL support this, but it would be the SQL standard way of doing this:

SELECT * FROM bb_bulletin WHERE (OfficeCode, IssuerId, BulletinDtm) = (20001, 1, '2011-01-07 14:04:40');

The other part is referring to the primary key columns using an abbreviated syntax. I don't know of any such possibility.

醉态萌生 2024-10-20 19:16:28

不,无论是在 MySQL 还是在我所知道的任何 SQL 方言中都没有这样的方法。

您应该在 PHP 中拆分 20001-1-2011-01-07 14:04:40 字符串,并使用其部分来构建 MySQL 查询。

我还可能补充一点,复合主键可能不是最好的想法性能明智(特别是对于 InnoDB 表)

此外 INT(5) 仍然占用与 INT(11)< 一样多的空间/code> (或简单的 INT )。对于较小的整数类型,请使用 TINYINTSMALLINTMEDIUMINT


笨拙的解决方法部分

下面的解决方案应该按照您想要的方式工作,但以资源和/或性能为代价。除非您确实无法采用最简单的解决方案,否则不应使用这些解决方案。


一种可怕的做法是这样的
WHERE CONCAT(OfficeCode,IssuerId,BulletinDtm) = '20001-1-2011-01-07 14:04:40'

这是可怕,因为它不允许 MySQL使用索引实际上可以加快查询速度。

请不要这样做。


另一种方式。将 CHAR(32) 列添加到您的表中,并将其设为您的 PK。在其中存储之前 PK 列的 MD5 哈希值(即 MD5('20001-1-2011-01-07 14:04:40')。然后您可以像这样查询: WHERE newPKcolumn = MD5('20001- 1-2011-01-07 14:04:40') 这将允许你做你想做的事,并且 MySQL 不再规范化表,但非规范化是你需要的权衡。有时为了提高性能或可用性而这样做并没有什么错。

No, there is not such a way, either in MySQL, or in any SQL dialect I know.

You should split 20001-1-2011-01-07 14:04:40 string in your PHP and use its parts to build your MySQL query.

I might also add that, composite primary key might not be the best idea performance wise (especially with InnoDB tables)

Also INT(5) still takes just as much space as INT(11) (or plain INT for that matter). For smaller integer types use TINYINT, SMALLINT and MEDIUMINT


Clumsy Workarounds section

Solutions below should work the way you want, but at the cost of resources and/or performance. Unless you really can't go with the simplest solution, you should not use these.


A horrible way to do it would be like this
WHERE CONCAT(OfficeCode,IssuerId,BulletinDtm) = '20001-1-2011-01-07 14:04:40'

It is HORRIBLE because it will not allow MySQL to use the index to actually speed up the query.

Don't do this, please.


Another way. Add a CHAR(32) column to your table and make it your PK. Store in it a MD5 hash of your previous PK columns (i.e. MD5('20001-1-2011-01-07 14:04:40'). Then you can query like: WHERE newPKcolumn = MD5('20001-1-2011-01-07 14:04:40'). This will allow you to do what you want, and MySQL to use the index. The table is no longer normalised, but denormalisation is a tradeoff you need to do sometimes to improve performance or usability. There's nothing wrong with that.

美人如玉 2024-10-20 19:16:28

您可以创建一个存储过程,将 '20001-1-2011-01-07 14:04:40' (字符串)作为参数,然后解析它并执行 SELECT< /code> 程序内的语句。

You could create a stored procedure that takes '20001-1-2011-01-07 14:04:40' (a string) as an argument, then parse it and make the SELECT statement inside the procedure.

ˉ厌 2024-10-20 19:16:28

不可能那样做。预解析的存储过程是完成此任务的一种方法。如果您不必坚持此表设计,我建议将主键更改为新列,您可以将其设置为自动增量。

如果您必须坚持这种设计,那么您仍然可以添加一个新的“映射”表,顾名思义,它将您的组合映射到主键:

CREATE TABLE IF NOT EXISTS `bbb_mapping` (
  `YourPK` int(11) NOT NULL AUTO_INCREMENT,
  `OfficeCode` int(5) NOT NULL,
  `IssuerId` int(11) NOT NULL,
  `BulletinDtm` datetime NOT NULL
  PRIMARY KEY (`YourPK`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

使用这种方法,您可以在使用 YourPK 时将映射表与原始表连接起来查询字符串。

干杯

No possible to do it like that. A stored procedure to pre-parse is one way to get this done. If you do not have to stick with this table design the I would suggest to change the primary key to a new column which you can set to autoincrement.

If you have to stick to this design then you can still add a new 'mapping' table which would as the name suggests map your combination to a primary key:

CREATE TABLE IF NOT EXISTS `bbb_mapping` (
  `YourPK` int(11) NOT NULL AUTO_INCREMENT,
  `OfficeCode` int(5) NOT NULL,
  `IssuerId` int(11) NOT NULL,
  `BulletinDtm` datetime NOT NULL
  PRIMARY KEY (`YourPK`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Using this approach you can join the mapping table with your original table while using YourPK in the querystring.

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文