I have 2 tables as follows:
Table: Visitor
---------------------
| Code |Contr|
|-------------------|
|225919528553 | 1003|
|-------------------|
|130324727131 | 1004|
|-------------------|
|353952972425 | 1010|
|-------------------|
|997498622785 | 1014|
|-------------------|
|
|
|
Table: Products
-----------------------------
| Code | Name| Color |
|----------------------------
|225919528553 | Pen | Balck |
|----------------------------
|130324727131 | Book| White |
|----------------------------
|353952972425 | Fan | Black |
|----------------------------
|997498622785 | DVD | Red |
|----------------------------
|
|
|
.
I want to more value of the "Contr" column following is displayed:
| Code | Name| Color | Contr |
|------------------------------------
|997498622785 | DVD | Red | 1014 |
|------------------------------------
|353952972425 | Fan | Black | 1010 |
|------------------------------------
|130324727131 | Book| White | 1004 |
|------------------------------------
|225919528553 | Pen | Balck | 1003 |
|------------------------------------
|
|
https://stackoverflow.com/revisions/f6f23a85-d8b8-4900-b72a-8ccb7b8abf25/view-source
try with
SELECT p.*, v.Contr
FROM Visitor AS v
JOIN Products AS p ON p.Code = v.Code
I would find more value in the table!?
select v.Code,
p.Name,
p.Color,
p.Contr
from
Visitor as v,
Products p
where
v.code=p.code order by contr
Related
I have the following database schema:
Table: products
| id | name | content |
|----|--------|---------|
| 1 | Pen | ... |
| 2 | Pencil | ... |
| 3 | Rubber | ... |
| 4 | Ruler | ... |
Table: feature_types
| id | name |
|----|----------|
| 1 | Color |
| 2 | Material |
| 3 | ... |
| 4 | ... |
Table: features
| id | product_id | feature_type_id | value |
|----|------------|-----------------|-----------|
| 1 | 1 | 1 | Red |
| 2 | 1 | 2 | Aluminum |
| 3 | 2 | 1 | Green |
| 4 | 2 | 2 | Wood |
| 5 | 3 | 1 | White |
| 6 | 4 | 2 | Plastic |
My question is how can I do something like this:
SELECT *, ... FROM products ...
With result:
| id | name | content | feature_type_1 | feature_type_2 |
|----|--------|---------|----------------|----------------|
| 1 | Pen | ... | Red | Aluminum |
| 2 | Pencil | ... | Green | Wood |
| 3 | Rubber | ... | White | NULL |
| 4 | Ruler | ... | NULL | Plastic |
So as you see, in results we have all columns from products table and additional columns for specified feature_types. Column names correspond to their identifiers, according to the pattern: "feature_type_{ID}".
I know feature_types IDs so it is not necessary to add all possible columns feature_types. I need only 2 additional columns with ID 1 and 2.
If you are only interested in the features: Color and Material, join the tables and group by product:
select
p.id, p.name, p.content,
max(case when t.name = 'Color' then f.value end) Color,
max(case when t.name = 'Material' then f.value end) Material
from products p
left join features f on f.product_id = p.id
left join feature_types t
on t.id = f.feature_type_id and t.name in ('Color', 'Material')
group by p.id, p.name, p.content
I guess in your sample data you did a mistake by setting 1 instead of 2 as feature_type_id for Plastic in the table features.
See the demo.
Results:
| id | name | content | Color | Material |
| --- | ------ | ------- | ----- | --------- |
| 1 | Pen | ... | Red | Aluminium |
| 2 | Pencil | ... | Green | Wood |
| 3 | Rubber | ... | White | |
| 4 | Ruler | ... | | Plastic |
Here's one solution to the part of the problem with which you are struggling...
SELECT product_id
, MAX(CASE WHEN feature_type_id = 1 THEN value END) feature_type_1
, MAX(CASE WHEN feature_type_id = 2 THEN value END) feature_type_2
FROM features
GROUP
BY product_id;
+------------+----------------+----------------+
| product_id | feature_type_1 | feature_type_2 |
+------------+----------------+----------------+
| 1 | Red | Aluminium |
| 2 | Green | Wood |
| 3 | White | NULL |
| 4 | Plastic | NULL |
+------------+----------------+----------------+
4 rows in set (0.03 sec)
or...
SELECT f1.product_id
, f1.value feature_type_1
, f2.value feature_type_2
FROM features f1
LEFT
JOIN features f2
ON f2.product_id = f1.product_id
AND f2.feature_type_id = 2
WHERE f1.feature_type_id = 1;
a semplified way is based on building a string using group_concat
select p.id, p.name, p.content , group_concat( concat(t.name,':',f.value )) all_features
from products p
inner join features f on f.product_id = p.id
inner join feature_types t on t.id = f.feature_type_id
group by p.id
Supposing I've 3 SQL tables like this:
Table fruits:
|----------|------------|
| fruit_id | fruit_name |
|----------|------------|
| 1 | Banana |
| 2 | Apple |
| 3 | Pear |
|----------|------------|
Table colors:
|----------|------------|
| color_id | color_name |
|----------|------------|
| 91 | Yellow |
| 92 | Green |
| 93 | Red |
|----------|------------|
Table associatives:
|----------|----------|
| fruit_id | color_id |
|----------|----------|
| 1 | 91 |
| 1 | 92 |
| 2 | 91 |
| 2 | 92 |
| 2 | 93 |
| 3 | 93 |
|----------|----------|
How can I list the fruits and the associates colors like this:
|----------|----------|
| fruit_id | color_id |
|----------|----------|
| 1 | 91,92 |
| 2 | 91,92,93 |
| 3 | 93 |
|----------|----------|
What I tried:
SELECT
fruit_id,
GROUP_CONCAT(COALESCE(color_id, "0")) AS color_id
FROM fruits
JOIN associatives
LEFT JOIN colors
ON FIND_IN_SET(fruit_id, color_id)
GROUP BY fruit_id
You only need a LEFT JOIN if you have fruits without colors. But your sample data doesn't show that. Anyway I adapt the sample from #Hackerman to include another fruit to show how to handle that case
SQL DEMO
select f.fruit_id, GROUP_CONCAT( COALESCE (a.color_id, 0) ) as color_id
from fruits f
left join associatives a
ON f.fruit_id = a.fruit_id
group by f.fruit_id
You just need to query your associates table in order to get the desired result:
select fruit_id, GROUP_CONCAT(color_id) as color_id
from associatives
group by fruit_id
Demo:
SQLFiddle demo
I have two tables, A and B
table A has this columns:
id, title
table B has these columns:
id, content, A_id, type
In table B, A_id is foreign key related to table A.
I want to write a search query that search title in A and left Join to B where A.id = B.A_id and search B.content
It is so easy:
SELECT A.*, B.content FROM A LEFT JOIN B ON A.id=B.A_id
WHERE A.title like 'sometitle' AND B.content like 'somecontent';
But I have column 'type' in table B that have entries like this: 'good','bad','ugly','good','good'...
when I search 'content' in table B I want that every content to be related to a 'type'. In other words, the query should give me result related to content1 and type good And content2 and type bad...
Example:
TAble A:
+----+--------+
| id | title |
+----+--------+
| 1 | white |
| 2 | blue |
| 3 | red |
| 4 | white |
| 5 | blue |
+----+--------+
table B:
+----+---------+------+------+
| id | content | A_id | type |
+----+---------+------+------+
| 1 | dog | 1 | good |
| 2 | dog | 1 | bad |
| 3 | cat | 2 | good |
| 4 | cat | 2 | bad |
| 4 | cat | 2 | ugly |
| 6 | crow | 3 | good |
| 7 | crow | 3 | bad |
| 8 | crow | 3 | ugly |
| 9 | mouse | 2 | good |
| 10 | zebra | 3 | bad |
| | | | |
+----+---------+------+------+
I want a query that that its output be this:
+----+-------+---------+------+
| id | title | content | type |
+----+-------+---------+------+
| 1 | white | dog | good |
| 2 | blue | cat | bad |
+----+-------+---------+------+
SQL HERE
Your query for the expected result was almost correct, just you used the wrong column content whether the correct column would be type. Look at the below query :
SELECT A.id, A.title, B.content, B.type FROM A
LEFT JOIN B ON A.id=B.A_id
WHERE (a.title='white' AND B.type='good')
OR (a.title='blue' AND B.type='bad')
ORDER BY a.id asc
OUTPUT
+----+-------+----------+------+
| id | title | content | type |
+----+-------+----------+------+
| 1 | white | content5 | good |
| 3 | blue | content6 | bad |
| 4 | white | content3 | good |
| 5 | blue | content9 | bad |
+----+-------+----------+------+
I think the query you posted at the end of your question should start like this:
SELECT A.id, A.title, B.A_id, B.content, B.type .... (etc.)
so that also B.A_id is fetched
Please be assured that I searched a lot on SE for an answer similar to mine but didn't get any good result and here I am asking for some help.
I have 3 tables as follows:
Table Professors:
+---------+--------+
| idProf | name |
+---------+--------+
| 1 | Ben |
| 2 | John |
| 3 | Bob |
+---------+--------+
Table Classes:
+---------+--------+------------+
| idClass | name | profRefId |
+---------+--------+------------+
| 1 | French | 1 |
| 2 | English| 1 |
| 3 | German | 3 |
| 4 | Science| 2 |
+---------+--------+------------+
Table Lessons:
+----------+----------+--------------+
| idLesson | name | classRefId |
+----------+----------+--------------+
| 1 | Lesson1 | 1 |
| 2 | Lesson2 | 1 |
| 3 | Lesson3 | 2 |
| 4 | Lesson4 | 4 |
| 5 | Lesson5 | 4 |
| 6 | Lesson6 | 3 |
+----------+----------+--------------+
Now, what I was struggling to achieve is:
I pass idProf as a URL parameter ($_GET['idProf'])
And I would like the right SQL statement based on that param to list all the classes for that professor and inside each class list its lessons.
Something that will look like this on a webpage:
Something like this?
SELECT a.name ProfessorName, b.name ClassName, c.name LessonName
FROM Professors a
INNER JOIN Classes b
ON a.idProf=b.profRefID
INNER JOIN Lessons c
ON b.idClass=c.classRefID
I have two tables like below,
mysql> select * from Books ;
+----+------+------------+----------+----------+
| id | name | author_name| category | category2|
+----+------+------------+----------+----------+
| 1 | 1 | Steve | CT001 | CT003 |
| 2 | 2 | John | CT002 | CT002 |
| 3 | 3 | Larry | CT003 | CT002 |
| 4 | 3 | Michael | CT004 | CT004 |
| 5 | NULL | Steven | CT005 | CT005 |
+----+------+------------+----------+----------+
mysql> select * from Codemst ;
+----+------+------------+
| id | code | name |
+----+------+------------+
| 1 | CT001| fiction |
| 2 | CT002| category1 |
| 3 | CT003| etc |
| 4 | CT004| etc2 |
| 5 | CT005| etc3 |
+----+------+------------+
I want to get human readable category name when I query like "select * from Books;"
If there was only one category in the Books table, I think I can use "Join" but, in this case what can I do?
select * from Books b
Inner Join Codemst c1 on b.category = c1.code
inner join codemst c2 on b.category2 = c2.code;
c1.name will hold the readable category, and c2.name the readable category2