How MySQL conditions the select result value of another table and queries

Asked 2 years ago, Updated 2 years ago, 117 views

Here's what I want to do.

Example A) select col_name from table_a where col_name like '%val%';

Result A) val1 val2 val3

Example B) select col, count(col) from table_bwhere color '%result A value 1%' OR color '%result A value 2%'...group by ...

Result B) val1 | 1 val2 | 12 val3 | 44

To add more explanation, select for one column in Table A. There are dozens of these results and they are included in the values of a particular field in Table B. Eventually, you need to extract how many values each of Table B has taken from Table A. You can get it by spinning dozens of repetitive statements in php, but I'm asking because I want to write better SQL statements.

mysql querying

2022-09-22 14:06

2 Answers

There are many ways, but I think we can use subquery.

select col_name, count(*) 
from  table_b
where col_name in (
            select col_name 
            from table_a 
            where col_name like '%val%'
)
group by col_name;


2022-09-22 14:06

It's hard to explain because I don't know the field name, but search group by and use it. Group by is a method of extracting data by grouping specific fields.


2022-09-22 14:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.