phpmyadmin查询使用多个表

时间:2017-03-09 02:49:42

标签: mysql sql

我很简单我的数据库中有2个表 1.发布 2.用户

Post表中的列是 User ID Post Title Post Content User ID

然后是User表中的列 Post.User ID - 已连接到User Full Name dataSource = new kendo.data.DataSource({ batch: true, pageSize: 20, transport: { read: { url: 'SaladEntry/GetSupport2/', dataType: "json" }, destroy: { url: 'SaladEntry/DeleteSupportKendo2/', type: "POST", contentType: 'application/json' }, create: { url: 'SaladEntry/SaveSupportKendo2/', type: "POST", contentType: 'application/json', complete: function (e) { SupportGrid(); } }, update: { url: 'SaladEntry/EditSupportKendo2/', type: "POST", contentType: 'application/json', complete: function (e) { SupportGrid(); } }, parameterMap: function (options, operation) { if (operation == "read") { return saladparamObj; } else { options.models[0].CountryNo = $('#Country_No').val(); var SaladParamSerialized = JSON.stringify(options.models); return SaladParamSerialized; } } }, schema: { model: { id: "PK", fields: { CountryNo: { editable: true, nullable: true }, EffectiveDate: { type: "date" }, stringEffectiveDate: { type: "string" }, ScaleMin: { validation: { required: true } }, ScaleMax: { validation: { required: true } }, Currency: { type: "string" } } } }, sort: { field: "stringEffectiveDate", dir: "desc", compare: function (a, b, dir) { return kendo.parseDate(a.EffectiveDate) - kendo.parseDate(b.EffectiveDate); } } }); $("#grid").kendoGrid({ dataSource: dataSource, pageable: true, height: 300, toolbar: ["create"], columns: [ { field: "stringEffectiveDate", title: "Date", template: "#= kendo.toString(kendo.parseDate(EffectiveDate, 'yyyy-MM-dd'), 'yyyy-MM-dd') #", width: "100px", sortable: { allowUnsort: false, compare: function (a, b, dir) { return kendo.parseDate(a.EffectiveDate) - kendo.parseDate(b.EffectiveDate); } } }, { field: "EffectiveDate", title: "Effective Date", template: "#= kendo.toString(kendo.parseDate(EffectiveDate, 'yyyy-MM-dd'), 'yyyy-MM-dd') #", width: "100px" }, { field: "ScaleMin", title: "BG1 Min", width: "100px" }, { field: "ScaleMax", title: "BG7 Max", width: "100px" }, { field: "Currency", title: "Currency", width: "100px" }, { command: ["edit", "destroy"], title: "Commands", width: "160px" } ], height: 300, scrollable: true, sortable: true, filterable: true, pageable: true, resizable: true, selectable: true, editable: "popup", cancel: function (e) { nEdit(); }, edit: function(e) { e.container.find(".k-edit-label:first").hide(); e.container.find(".k-edit-field:first").hide(); if (!e.model.isNew()) { var dt = e.container.find("input[name=EffectiveDate]").data("kendoDatePicker"); dt.enable(false); } } }).css("background-color", "#C7D6A7");

然后我现在要做的是选择Juan Cruz发布的帖子(User.User Full Name)。请帮助,我无法在任何地方找到这些问题。

PS抱歉我的英文不好。

编辑:我将在我的php / html项目中使用它作为我的搜索模块。所以我需要知道Juan Cruz发布的Juan Cruz用户ID的帖子。再次感谢

2 个答案:

答案 0 :(得分:0)

您需要使用 JOIN 。这应该更新为使用您的列名。

SELECT p.id, p.title, p.content, u.full_name FROM User AS u JOIN Post AS p ON (u.id = p.user_id);

答案 1 :(得分:0)

希望这可能有所帮助:

SELECT
    p.post_id, p.user_id, p.post_title, p.post_content,
    u.user_fullname     
    // column name from two or more tables
    // eg: table_name.column_name
    // use p and u, because we already give temporary name for table post and user
FROM
    post p          // table 1: post refer as p (SQL aliases)
JOIN                // combine 2 column from two or more tables
    user u          // table 2: user refer as u (SQL aliases)
ON
    p.user_id = u.user_id   // key for joining the table, the ID should be the same

或者您可以使用以下代码而不使用别名:

SELECT
    post.post_id, post.user_id, post.post_title, post.post_content,
    user.user_fullname
FROM
    post
JOIN
    user
ON
    post.user_id = user.user_id

参考:SQL JOIN