按外键搜索(Search by foreign key)
我正在开发一个允许用户搜索音乐,艺术家,专辑和混音带的应用程序。 数据库有四个表:音乐,艺术家,混音带,专辑。 3个表; 混音带,音乐和专辑与艺术家在名为artist_id的列中使用外键相关。 通过使用如下查询从三个表中的每个表中获取搜索结果非常容易:
$music = "SELECT * FROM music WHERE song_title LIKE '%$search_term%'";
然而,我发现获得专辑,混音带和音乐的结果有点令人生畏,其中artist_name =搜索词,因为艺术家的参考不是通过名称而是通过artist_id制作我如何返回音乐,专辑和混音带的结果用户提供的搜索词(即艺术家)?
I’m working on an application that allows users to search for music, artists, albums and mixtapes. The DB has these four tables: Music, Artists, Mixtapes, Albums. The 3 tables; mixtapes, music and albums are related to the artist using foreign keys in a column called artist_id. It is quite easy to get search results from each of the three tables by using a query such as this:
$music = "SELECT * FROM music WHERE song_title LIKE '%$search_term%'";
However, I’m finding it a bit daunting to get results for albums, mixtapes and music where artist_name = search term since the artist’s reference is not made by name but rather by artist_id How can I return results for music, albums and mixtapes for a user supplied search term (ie. artist)?
原文:https://stackoverflow.com/questions/19907531
最满意答案
如果从数据库设计的角度考虑这个问题,那么拥有一个具有以下参数的
BaseballPlayer
对象可能会很奇怪:
- 球队
- 位置
- 对手
- 大约10或11个统计数据(历史)
- 约10或11个统计数据(预计)
- 投手对决
- 天气
因为某些特定的东西与特定的
BaseballPlayer
保持相对固定,例如名称等,但这些其他东西是流动的和短暂的。如果您将其设计为具有各种数据库表的应用程序,那么,此处列出的每个事物都可能代表一个单独的表,并且
BaseballPlayer
与这些其他表的关系相当于当前和以前的Team
等。因此,我可能会将问题分解为更多的类,包括
StatsClass
和Team
类(这可能是Opponent
真正的......)。但这一切都取决于你想做什么。 通常当您向后弯曲以将数据塞入结构或执行相同操作以将其取回时,可以重新设计设计以使您的工作更轻松。
If you think about this from the perspective of database design, it would probably be odd to have a
BaseballPlayer
object that has the following parameters:
- Team
- Position
- Opponent
- about 10 or 11 stats (historical)
- about 10 or 11 stats (projected)
- pitcher matchup
- weather
Because there are certain things associated with a particular
BaseballPlayer
which remain relatively fixed, such as name, etc., but these other things are fluid and transitory.If you were designing this as an application with various database tables, then, it's possible that each of the things listed here would represent a separate table, and the
BaseballPlayer
's relationship with these other tables amount to current and formerTeam
, etc.Thus, I would probably break up the problem into more classes, including a
StatsClass
and aTeam
class (which is probably what anOpponent
really is...).But it all depends what you would like to do. Usually when you are bending over backwards to cram data into a structure or doing the same to get it back out, the design could be reworked to make your job easier.
相关问答
更多-
else块只在try中的代码没有引发异常时才执行; 如果你把代码放在else块之外,它会发生,不管有没有例外。 此外,它发生在finally ,这通常很重要。 当您有一个可能出错的简短设置或验证部分时,这通常很有用,其次是您使用您设置的资源的块,您不想隐藏错误。 你不能把代码放在try因为当你希望它们传播时,错误可能会发送到except子句。 你不能把它放在构造之外,因为那里的资源肯定不可用,要么是因为安装失败,要么是因为finally把所有东西都撕掉了。 因此,你有一个else块。 The else bl ...
-
传递NULL而不是任何东西 例: $obj = new Whatever($parm1,$parm2,$parm3,NULL,NULL,NULL); 或者在构造函数中将其设置为null __construct($parm1 = NULL,$parm2 = NULL,$parm3 = NULL,$parm4 = NULL,$parm5 = NULL,$parm6 = NULL) 并称之为这样 $obj = new Whatever(); 您的评论更新: 你的代码: function __con ...
-
UriBuilder builder = new UriBuilder() .setScheme("https") .setHost("www.google.com") .setPath("/p1/p2") .addParameter("what",null) .addParameter("k1", "v1") .addParameter("k2", "v2") builder.toString(); UriBuilder builder = new Uri ...
-
您还可以使用随播对象来定义替代构造函数: case object A { def apply(conf: Conf): A = new A(conf.get("x"), conf.get("y")) } case object B { def apply(conf: Conf): B = new B(conf.get("x"), conf.get("y"), conf.get("z")) } After looking around some more I found this (also ...
-
如果从数据库设计的角度考虑这个问题,那么拥有一个具有以下参数的BaseballPlayer对象可能会很奇怪: 球队 位置 对手 大约10或11个统计数据(历史) 约10或11个统计数据(预计) 投手对决 天气 因为某些特定的东西与特定的BaseballPlayer保持相对固定,例如名称等,但这些其他东西是流动的和短暂的。 如果您将其设计为具有各种数据库表的应用程序,那么,此处列出的每个事物都可能代表一个单独的表,并且BaseballPlayer与这些其他表的关系相当于当前和以前的Team等。 因此,我可能会 ...
-
代替 paramObj = { _pick:pick } 做就是了 paramObj._pick = pick; 你正在做的当前方式, paramObj对象只是被重置每个if语句。 Instead of paramObj = { _pick:pick } just do paramObj._pick = pick; The current way you're doing it, the paramObj object is just being reset each if stat ...
-
您无法直接实例化PDOStatement。 它是硬编码的,不允许它。 我在PHP 5.5源代码中找到了这个,在ext / pdo / pdo_stmt.c中: static PHP_FUNCTION(dbstmt_constructor) /* {{{ */ { php_error_docref(NULL TSRMLS_CC, E_ERROR, "You should not create a PDOStatement manually"); } /* }}} */ 您应该通过调用PDO ...
-
如果您正在尝试构建实体框架连接字符串,请使用如下。它将帮助您.. string connectionString = new System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(connec ...
-
您始终可以将多个参数类型打包到元组中。 要打包整数值,可以使用类似值转换器,如下所示: template
class TypeValue { public: static const size_t value = N; }; template const size_t TypeValue ::value; #include /// Or using testing::Test; using testing::T ... -
datetime.date是一个不可变类型,这意味着您需要覆盖__new__方法 : class Fecha(datetime.date): def __new__(cls, year, month, day, status): instance = super(Fecha, cls).__new__(cls, year, month, day) instance.status = status return instance 演示: >>> imp ...