I need help getting the nth number of a result in MySQL.
I have three tables with the following fields:
table1: product_id, name, description
table2: category_id, name, description
table3: product_id, category_id
I'm trying to get this result:
name | description - product_category -category2 - category3
___________________________________________________________________
NAME 1 | Description 1 | Cat1 | Cat2 | Cat5
NAME 2 | Description 2 | Cat7 | Cat9 | Cat11
The query I'm trying to use is as follows:
SELECT
t1.name AS product_name,
t1.description AS product_description,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 1) AS product_category,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 2) AS product_category2,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 3) AS product_category3
FROM table1 t1 INNER JOIN
table3 t3
ON t3.product_id = t1.product_id INNER JOIN
table2 t2
ON t2.category_id = t3.category_id
;
And sadly I get this result:
name - description - category
NAME 1 - Description 1 - Category 1
NAME 2 - Description 2 - Category 2
Try using LEFT JOIN instead of INNER JOIN.
Example:
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2
Related
I have 3 tables in my Mysql DB as follows:
daily_data_update:
----------------------------------------------
id date code user_id followup_status
----------------------------------------------
1 08/10/2020 AD123 1 1
2 08/10/2020 AD134 2 2
3 08/10/2020 AD123 1 1
---------------------------------------------
users:
-------------------------------------
id first_name user_region
------------------------------------
1 Peter scotland
2 Susan ireland
------------------------------------
followupstatus
------------------------------------
id followup_status
---------------------------------------
1 Paid
2 Unpaid
----------------------------------------
I need to select date,code and followup_status from daily_data_update WHERE user_id in users along with followup_status. My query is as follows:
SELECT t1.`date`,
t1.`code`,
t3.`followup_status`
FROM `daily_data_update` AS t1,
followupstatus AS t3
LEFT OUTER JOIN users AS t2 ON t1.user_id = t2.id
WHERE t2.id IS NOT NULL
AND t2.user_region = 'scotland'
But the query is failing as Unknown column 't1.user_id' in 'on clause'
What is wrong with my query and how can i resolve it ?? Requesting help..
Try this query:
SELECT t1.`date`,
t1.`code`,
t3.`followup_status`
FROM `daily_data_update` AS t1
LEFT JOIN followupstatus AS t3 ON t1.followup_status = t3.id
LEFT JOIN users AS t2 ON t1.user_id = t2.id
WHERE t2.id IS NOT NULL
AND t2.user_region = 'scotland'
Hope this works!!
I have a table with values look like this.
Id Name Fruit
------------
1 Jon Apple
------------
2 Jon Orange
------------
3 Jon Grape
------------
4 Mike Apple
------------
5 Mike Orange
-------------
How to distinct the column into something like this in mysql?
Name Fruit
----------
Jon Apple
Orange
Grape
-----------
Mike Apple
Orange
-----------
This should do
SELECT name, GROUP_CONCAT(fruit SEPARATOR '\n') FROM your_table GROUP BY name
Demo in db<>fiddle
Update to add numbering:
SELECT name ,
GROUP_CONCAT(CONCAT (rn,')',fruit) SEPARATOR '\n')
FROM (
SELECT *
,ROW_NUMBER() OVER (PARTITION BY name) AS rn
FROM your_table
) SQ
GROUP BY name
Demo with numbering in db<>fiddle
Another option is also using joins to achieve this requirement.
select
case when t1.id = t2.id then t1.name else '' end
, t3.fruits
from
(select min(id) id, name from testA
group by name) t1
left join
testA t2 on t2.name = t1.name
left join
testA t3 on t3.id = t2.id
order by t2.id asc
see dbfiddle.
I have two tables:
id1, id2, ... idX contains id from t2 or null. Can I make such select, which will put params from t2 in t1?
For example i have a t1(name,year,id1,id2,id3,id4) values:
name year id1 id2 id3 id4
------------------------------------------
marko 1999 1 2 null 1
polo 1985 null null 5 3
And t2 values:
id info param
--------------------------------------
1 apple green
2 car yellow
3 bee pink
4 doctor whiskey
5 book small
So I'd like to have such dynamical query results, based on t1 rows:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
SELECT name, year, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
I googled a lot, but found nothing except nested queries, such as:
SELECT
name,
year,
(SELECT `info` FROM t2 WHERE id = t1.id1) AS id1-info,
(SELECT `param` FROM t2 WHERE id = t1.id1) AS id1-param,
(SELECT `info` FROM t2 WHERE id = t1.id2) AS id2-info...
But I understand, that it is a very bad idea, because I have a lot of id columns in t1, which are not static. Or if it cannot be done dynamically, can I just make SELECT, which will show me all in one row:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
To use one select in one row, you can use mulitple left join on t2.
SELECT name,
`year`,
id11.info as `id1-info`,
id11.param as `id1-param`,
id21.info as `id2-info`,
id21.param as `id2-param`,
id31.info as `id3-info`,
id31.param as `id3-param`,
id41.info as `id4-info`,
id41.param as `id4-param`
FROM t1
LEFT JOIN t2 as id11 on t1.id1=id11.id
LEFT JOIN t2 as id21 on t1.id2=id21.id
LEFT JOIN t2 as id31 on t1.id3=id31.id
LEFT JOIN t2 as id41 on t1.id4=id41.id
WHERE name = 'marko';
result:
name year id1-info id1-param id2-info id2-param id3-info id3-param id4-info id4-param
marko 1999 apple green car yellow (null) (null) apple green
I need to find all attributes & image(s) for a product.
Every product is marked as such in the column type in Table 1.
The attributes are also defined in Table 1, each attribute on a seperate row.
This is a simplified version of the table structures:
Table 1:
id | type | value | url
1 | product | T-shirt |
2 | image | | http://www......
........
15 | size | XXL |
........
18 | color | blue |
Table 2:
id | ref_id | link_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 15
4 | 1 | 18
The relationship between a product and its attributes is defined in Table 2:
- ref_id matches the id in Table 1 for the product we want
- link_id holds an id (from Table 1) which is either the product itself or an attribute of the product
So in this example, id 1 in Table 1 has 4 occurences in Table 2 (ref_id = 1): the product row and 3 attribute rows (image, size & color)
They are referenced by the column link_id in Table 2.
I would like to solve this in 1 query if possible, but I am stuck at how to solve the 'back-reference'.
Once I have found a product's id in Table 1, I can get the link_id's in Table 2 allright, but how do I use those again (in the same query) to get the rows from Table 1.
Is this at all possible in 1 query, or should I use 2 seperate queries for this?
UPDATE:
this is how far I have come:
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id
FROM table_1 t1
LEFT JOIN table_2 t2
ON t1.id = t2.ref_id
WHERE t1.type = 'product'
But after that I don't know how to construct the rest of the query based on the 'back-reference'.
You can join back to table_1 again
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id, t1_attributes.type, t1_attributes.value
FROM table_1 t1
LEFT JOIN table_2 t2
ON t1.id = t2.ref_id
LEFT JOIN table_1 t1_attributes ON t2.link_id = t1_attributes.id
WHERE t1.type = 'product'
This will get you 1 row for each attribute though. If you want a field for each type of attribute, you'll have to do a join for each one.
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id, t1_image.value, t1_size.value
FROM table_1 t1
LEFT JOIN table_2 t2 ON t1.id = t2.ref_id
LEFT JOIN table_1 t1_image ON t2.link_id = t1_image.id
LEFT JOIN table_1 t1_size ON t2.link_id = t1_size.id
WHERE t1.type = 'product' AND t1_image.type = 'image' AND
t1_size.type = 'size'
I have a table as below
ID | CID
1 | 3
2 | 0
3 | 4
4 | 0
5 | 0
6 | 3
Below is the SQL query I use which is SELF JOIN.
SELECT t1.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
Which gives me O/P as below.
ID | CID
3 | 4
4 | 0
But what I want as an O/P is 1,3,4,6.
Logic of the O/P is Rows IDs or CIDs which are being used. If I explain more When the ID is 1 CID 3, When the ID is 3 CID is 4, When the ID is 6 CID is 3. When I get the unique IDs & CIDs that are used in the table would be 1,3,4,6.
Final Correct O/P Required is below.
ID
1
3
4
6
How can I get it done?
Not sure what you're trying to do. I think you are saying you want the ID of rows that have a non-zero CID or that are referenced by the CID column. (?) Try this:
SELECT ID FROM tbl_a AS t1 WHERE CID <> 0 OR EXISTS(SELECT * FROM tbl_a AS t2 WHERE t2.CID = t1.ID) ORDER BY ID
Try this
SELECT t2.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
OR t2.ID = t1.CID
GROUP BY t2.ID
I think this may be what you want:
select ID
from tbl_a
where id in (3, 4) or cid in (3, 4);