0

I'll fabricate data to illustrate my question: I have 2 tables, one called clients, and one called shippingProfiles.

                   clients                           shippingProfiles
    *************************************   *********************************
    * clientId username   emailAddress  *   * clientId  street     town     *
    * * * * * * * * * * * * * * * * * * *   * * * * * * * * * * * * * * * * *
    * 1        james      jdoer@aol.org *   * 1         mole       north    *
    * 2        zac        zmit@aol.org  *   * 1         maple      pleasant *
    * 3        cris       cm@yahoo.org  *   * 3         oak        brook    *
    * 4        john       jg@yahoo.org  *   * 3         taylor     glen     *
    *************************************   *********************************

clients.clientId is a unique primary key. Some of the clients have multiple shipping profiles, others have none.

I would like to search the database for any client that has the letter "m" in their username, emailAddress, street, or town. I would like each unique client to be returned as one entry (and only one entry), showing their id, username, email address, street, and town. If they don't have a street and town it can display null (or something similar), and if they have multiple streets and towns it can display any one of them.

This should be the result:

    *********************************************************
    * clientId  username  emailAddress    street  town      *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * 1         james     jdoer@aol.org   mole    north     *
    * 2         zac       zmit@aol.org    null    null      *
    * 3         cris      cm@yahoo.org    oak     brook     *
    *********************************************************

The tables may eventually hold 50,000 entries each so I need the database to do this in the least possible sweeps.

I've heard mysql is actually very powerful so I'm assuming this can be done with one command.

4

1 に答える 1

1

左外部結合が必要だと思います。これにより、行を他のすべての行に結合しようとしますが、結合が行われなかった場合でも、他のテーブルのフィールドにnullを入力するだけで、すべてのフィールドが出力されます。

クライアントから*を選択します。LEFTOUTERJOINshippingProfiles ON clients.clientId = ShippingProfiles.ID

WHERE projects.username LIKE'%m%'OR--ここに他の条件を挿入

于 2013-01-22T04:19:02.303 に答える