MySql: how to get the desired result

gesanri :

I've a table like this:

CREATE TABLE `base_build_floor` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `build_no` varchar(64) NOT NULL,
  `build_name` varchar(64) DEFAULT NULL,
  `floor_no` varchar(64) DEFAULT NULL,
  `floor_name` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

and insert some data:

INSERT INTO `base_build_floor` VALUES ('41', 'BUILD40210011', 'A', null, null);
INSERT INTO `base_build_floor` VALUES ('42', 'BUILD40210012', 'B', null, null);
INSERT INTO `base_build_floor` VALUES ('43', 'BUILD40210013', 'C', null, null);
INSERT INTO `base_build_floor` VALUES ('44', 'BUILD40210013', 'C', 'FLOOR40210002', 'C1');
INSERT INTO `base_build_floor` VALUES ('45', 'BUILD40210013', 'C', 'FLOOR40210003', 'C2');
INSERT INTO `base_build_floor` VALUES ('46', 'BUILD40210012', 'B', 'FLOOR40210004', 'B1');

the table is about a build-floor table, first you should make a building, then, a building can has no or some floors. the A building has no floor, the B building has one floor named B1, the C building has two floors named C1 and C2, I want to get the result as below:

41 BUILD40210011 A null null
44 BUILD40210013 C FLOOR40210002 C1
45 BUILD40210013 C FLOOR40210003 C2
46 BUILD40210012 B FLOOR40210004 B1

it means that, if a building has no floors, then get it, while if a building has any one floor, the building itself should not be got, so how to write the mysql?I've tried to use Subquery but doesn't work

I've try like this :

SELECT
    b.*
FROM
    base_build_floor b
WHERE
    b.floor_no IS NOT NULL
OR (
    b.floor_no IS NULL
    AND b.build_no NOT IN (
        SELECT
            GROUP_CONCAT(nostr)
        FROM
            (
            SELECT
                concat("'", f.build_no, "'") as nostr
            FROM
                base_build_floor f
            WHERE
                f.floor_no IS NOT NULL
            GROUP BY
                f.build_no
        ) t
)

)

but I get all the data

forpas :

With NOT EXISTS:

select t.* from base_build_floor t
where t.floor_no is not null
or not exists (
  select 1 from base_build_floor
  where build_no = t.build_no and floor_no is not null
)  

See the demo.
Results:

| id  | build_no      | build_name | floor_no      | floor_name |
| --- | ------------- | ---------- | ------------- | ---------- |
| 41  | BUILD40210011 | A          |               |            |
| 44  | BUILD40210013 | C          | FLOOR40210002 | C1         |
| 45  | BUILD40210013 | C          | FLOOR40210003 | C2         |
| 46  | BUILD40210012 | B          | FLOOR40210004 | B1         |

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP and mysql how to use joins to get the desired result

how to get the desired result of inheritance?

How to customize URL to get desired result with requests

How to get desired result in SQL Server

How to get desired result in the final answer

MySQL date between won't get desired result

How to write a mysql query to get the desired data?

How to get desired results in mysql query?

How to properly mix generics and inheritance to get the desired result?

How to use DurationFormatUtils class to get the result in desired format?

How to get my desired view result from one table

How to use an Inner Join to get desired result in SQL Server 2008

How to combine two lists to get the following desired result containing tuples?

MySQL GROUP BY not giving desired result

Query result into desired format (MySQL)

MySql query not giving desired result

Problems with query to get the desired result

Tkinter - Columnspan does not bring desired result: How to get widgets to their desired place?

How to get distinct result in mysql?

Logical expression in Mysql statement not giving desired result

Athena/SQL query to get the desired result

Can't get desired result on Java program

Excel : Pivot table not able to get desired result

Can't get result of desired query in linq

How do i get desired result by comparing array of object with object in Javascript?

how to get the desired result based on some certain condition in sql using case?

How to use lead or Lag for a Group in oracle or any other analytic function to get desired result?

JPA Criteria Query - How to implement Join on two tables to get desired result in single Query

How should i edit WHERE clause in SQL query to get desired result?