我的表$(document).on("input", "#PasswordField", function () {
if ($(this).val() !== "")
{
//TO DO
}
});
包含articles
和id
,表name
包含company
和id
以及数据透视表(name
)与article_company
,id
,article_id
和company_id
。
我想将一篇文章与公司联系起来,因此我需要一份包含所有产品的请求,以及他们是否与该公司相关联。
我尝试了这个查询:
price
但不幸的是我有来自articles table和company_article的两篇文章。
如果不是很清楚,我想要所有文章行,但如果文章在company_article中,我想要该表中的行。
编辑:数据表: - 文章:
SELECT
article_id,
name,
price AS price,
1 AS associated
FROM articles
JOIN article_company ON articles.id = article_company.article_id
WHERE company_id = 26
UNION
SELECT
id AS article_id,
name,
NULL AS price,
0 AS associated
FROM articles
GROUP BY article_id
-company_article:
+----+--------------+
| id | name |
+----+--------------+
| 1 | Aez |
| 2 | aze |
| 3 | za |
| 4 | azee |
| 5 | article test |
| 6 | test 2 |
| 7 | Test 3 |
+----+--------------+
我拥有的和我想要的行:
+------------+------------+-------+
| article_id | company_id | price |
+------------+------------+-------+
| 5 | 26 | 54.00 |
| 3 | 26 | 8.90 |
+------------+------------+-------+
答案 0 :(得分:0)
尝试这样的事情:
SELECT * FROM articles a LEFT OUTER JOIN company_article ca ON a.id =
ca.article_id;
这是左外连接的解释: http://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj18922.html
答案 1 :(得分:0)
好的,所以我发现自己这是我提出的要求:
SELECT article_id as id, name, price
res.associated as associated, created_at
FROM (
SELECT
article_id, name, price
1 AS associated, created_at
FROM articles
JOIN article_company ON articles.id = article_company.article_id
WHERE company_id = $id
UNION ALL
SELECT
id AS article_id, name, null as price
0 AS associated, created_at
FROM articles) AS res
GROUP BY article_id
ORDER BY associated DESC