During a period of time, the data and duplicate regions are not added to two tables.
The first table is f_city (field: city_id and field city_name)
The second table is f_chengjiao (field: id, city_id, area, taoshu, fang_time)
The associated field is city_id.
The statement I wrote is
SELECT CONCAT(fc.city_name), SUM(fcj.area), SUM(fcj.taoshu) FROM f_chengjiao AS fcj LEFT JOIN f_city AS fc USING (city_id) WHERE ".$sql." GROUP BY fcj.fang_time ORDER BY fc.city_id
The result is that the repeated regions are not merged and added.
Reply to discussion (solution)
The GROUP is group by fcj. fang_time.
Naturally, duplicate regions are not merged.
Because the same fc. city_name may have different fcj. fang_time values (obviously a time)
The GROUP is group by fcj. fang_time.
Naturally, duplicate regions are not merged.
Because the same fc. city_name may have different fcj. fang_time values (obviously a time)
Thanks for the prompt, but now there is another problem: in this statistical period, some regional data is not added because there is no data. how can I display 0 in the SQL statement for the region without adding data?
F_chengjiao AS fcj left join f_city AS fc
Change
F_chengjiao AS fcj right join f_city AS fc
Or
F_city AS fc left join f_chengjiao AS fcj
Thanks for the prompt, but now there is another problem: in this statistical period, some regional data is not added because there is no data. how can I display 0 in the SQL statement for the region without adding data?
Then you have to tell the database about the list of all your regions! This is to associate (left/right join) all your region lists based on the region field of your current result set.
F_chengjiao AS fcj left join f_city AS fc
Change
F_chengjiao AS fcj right join f_city AS fc
Or
F_city AS fc left join f_chengjiao AS fcj
It still cannot display the effect that the region with no data added is 0, which is the same as that before
Thanks for the prompt, but now there is another problem: in this statistical period, some regional data is not added because there is no data. how can I display 0 in the SQL statement for the region without adding data?
Then you have to tell the database about the list of all your regions! This is to associate (left/right join) all your region lists based on the region field of your current result set.
All regions exist in the first table f_city (field: city_id and field city_name ).
You want to group by city_name
You want to group by city_name
Yesby city_name
SELECT CONCAT(fc.city_name), SUM(fcj.area), SUM(fcj.taoshu) FROM f_chengjiao AS fcj RIGHT JOIN f_city AS fc USING (city_id) WHERE ".$sql." GROUP BY fc.city_name ORDER BY fc.city_id
SELECT * FROM region table
Left JOIN statistical table ON Region table. city_name = Statistical Table. city_name
Group by Region table. city_name
I don't know the difference between this USING (city_id) and on, so I didn't change it on your SQL!
SELECT * FROM region table
Left JOIN statistical table ON Region table. city_name = Statistical Table. city_name
Group by Region table. city_name
I don't know the difference between this USING (city_id) and on, so I didn't change it on your SQL!
USING (city_id) is short for table join fields
SELECT * FROM region table
Left JOIN statistical table ON Region table. city_name = Statistical Table. city_name
Group by Region table. city_name
I don't know the difference between this USING (city_id) and on, so I didn't change it on your SQL!
City_name is not an association field and cannot be found.
City_name is not an association field and cannot be found.
How does the data in your data table know which city it belongs to, whether it uses id or name? Use that field as the join condition!
City_name is not an association field and cannot be found.
How does the data in your data table know which city it belongs to, whether it uses id or name? Use that field as the join condition!
If it is used, it still does not show the effect of adding no data city to 0. only the result of adding data is displayed.
I understand this! Do you want this effect?
I understand this! Do you want this effect?
Sorry, the returned information is too late. the login account cannot be logged on after uppercase is enabled, and the account is locked.
Are you still using conditional statements? I don't understand the meaning of this conditional sentence. can you explain it?
I tried to use the method you wrote to modify it. because it requires a region sum, an error is reported after being wrapped in a condition statement. Another problem is, after Table Association, how can you search for city names without the table prefix?
1. are you still using the conditional statements here?
1) If your "condition statement" refers to where, you can add it after left join.
2) If your "condition statement" refers to the link condition after on, check my reply on the 12th floor, the goal is to let the data table know which city each data entry comes from the city table.
2. regional summation
SELECT city_name, IF (toushu is NULL, 0, sum (toushu ))
FROM t_city
Left join t_chengjiao on t_city.cid = t_chengjiao.cid
Group by t_chengjiao.cid
3. after table Association, how can you search for city names without the table prefix?
After the table is associated with the table, no fields with duplicate names appear. you can leave the prefix empty.
1. are you still using the conditional statements here?
1) If your "condition statement" refers to where, you can add it after left join.
2) If your "condition statement" refers to the link condition after on, check my reply on the 12th floor, the goal is to let the data table know which city each data entry comes from the city table.
2. regional summation
SELECT city_name, IF (toushu is NULL, 0, sum (toushu ))
FROM t_city
Left join t_chengjiao on t_city.cid = t_chengjiao.cid
Group by t_chengjiao.cid
3. after table Association, how can you search for city names without the table prefix?
After the table is associated with the table, no fields with duplicate names appear. you can leave the prefix empty.
The condition sentence I am talking about is IF (toushu is NULL, 0, sum (toushu.
I modified it again, but I still can't get it out.
SELECT CONCAT(fc.city_name), IF(fcj.area is NULL,0,SUM(fcj.area)), IF(fcj.taoshu is NULL,0,SUM(fcj.taoshu)) FROM f_city AS fc LEFT JOIN f_chengjiao AS fcj USING (city_id) WHERE ".$sql." GROUP BY fc.city_id ORDER BY fc.city_id
Result example: (if I reserve a city_id = 13, no data is added but not displayed)
Export the data. let me test it.
You cannot let me construct a data table based on your data table.
Export the data. let me test it.
You cannot let me construct a data table based on your data table.
-- PhpMyAdmin SQL Dump -- version 3.5.1 -- http://www.phpmyadmin.net---- Host: localhost -- generation date: September 13, 2014 -- Server version: 5.5.24-log -- PHP version: 5.3.13SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+ ";/*! 40101 SET @ OLD_CHARACTER_SET_CLIENT = @ CHARACTER_SET_CLIENT */;/*! 40101 SET @ OLD_CHARACTER_SET_RESULTS = @ CHARACTER_SET_RESULTS */;/*! 40101 SET @ OLD_COLLATION_CONNECTION = @ COLLATION_CONNECTION */;/*! 40101 set names utf8 */; ---- database: 'fang01' ---- structure of the orders TABLE 'F _ chengjiao '-- create table if not exists 'F _ chengjiao' ('id' mediumint (8) unsigned not null AUTO_INCREMENT, 'City _ id' mediumint (8) not null, 'iszhu' char (1) not null default '1' comment' // residential or non-residential, 1 is residential ', 'area 'Int (10) not null comment' // area ', 'taoshu' int (10) not null comment' // Set of number ', 'Fang _ time' date not null comment' // real estate information network warehouse receiving time', 'OS _ time' datetime NOT NULL COMMENT' // time for warehouse receiving to the computer ', primary key ('id') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 14; ---- convert the data in the table 'F _ chengjiao '-- insert into 'F _ chengjiao' ('id', 'City _ id', 'iszhu', 'area ', 'taoshu', 'fang _ time', 'OS _ time') VALUES (1, 1, '1', 100,200, '2017-09-01 ', '2017-09-01 00:00:00 '), (2, 2, '1', 2014, '2017-09-01', '2017 4-09-01 00:00:00 '), (3, 3, '1', 100,200, '2017-09-01', '2017-09-01 00:00:00 '), (4, 4, '1', 100,200, '2017-09-01 ', '2017-09-01 00:00:00'), (5, 5, '1', 2014, '2017-09-01 ', '2017-09-01 00:00:00'), (6, 6, '1', 2014, '2017-09-01 ', '2017-09-01 00:00:00 '), (7, 7, '1', 2014, '2017-09-01', '2017-09-01 00:00:00 '), (8, 8, '1', 100,200, '2017-09-01 ', '2017-09-01 00:00:00'), (9, 9, '1', 2014, '2017 -09-01 ', '2017-09-01 00:00:00'), (10, 10, '1', 2014, '2017-09-01 ', '2017-09-01 00:00:00 '), (11, 11, '1', 2014, '2017-09-01', '2017-09-01 00:00:00 '), (12, 12, '1', 100,200, '2017-09-01 ', '2017-09-01 00:00:00'), (13, 1, '1', 2014, '2017-09-08 ', '2017-09-01 00:00:00 '); -- ------------------------------------------------------------ TABLE structure 'F _ City' -- create table if not exists 'F _ City' ('City _ id 'Mediumint (8) not null, 'City _ name' varchar (10) not null, primary key ('City _ id') ENGINE = InnoDB default charset = utf8; ---- data in the table 'F _ City' -- insert into 'F _ City' ('City _ id', 'City _ name') VALUES (1, 'nanchang city '), (2, 'donghu District '), (3, 'xihu District'), (4, 'qingshanghu District '), (5, 'qingyunpu District'), (6, 'wanli region'), (7, 'economic development region'), (8, 'high-tech zone '), (9, 'honggutan region'), (10, 'sanhai region '), (11, 'Hero region'), (12, 'nanchang County '), (13, 'New statement ');/*! 40101 SET CHARACTER_SET_CLIENT = @ OLD_CHARACTER_SET_CLIENT */;/*! 40101 SET CHARACTER_SET_RESULTS = @ OLD_CHARACTER_SET_RESULTS */;/*! 40101 SET COLLATION_CONNECTION = @ OLD_COLLATION_CONNECTION */;
Check your where statement
Check your where statement
Where is the choice of a time period
$sql = '';if($start_time && $end_time){ $sql = " fcj.fang_time >='".$start_time."' AND fcj.fang_time<='".$end_time."' ";}else if($start_time){ $sql = " fcj.fang_time ='".$start_time."' ";}else if($end_time){ $sql = " fcj.fang_time ='".$end_time."' ";}else{ $sql = " fcj.city_id='".$city."' ";}
SELECT city_name, sum (area) as 'area ', sum (taoshu) as 'number' FROM f_city left join f_chengjiao on f_city.city_id = f_chengjiao.city_id group by f_city.city_id
The same is true for the right link.
SELECT city_name, sum (area) as 'area ', sum (taoshu) as 'number' FROM f_city left join f_chengjiao on f_city.city_id = f_chengjiao.city_id group by f_city.city_id
It's strange that the statement I wrote on the 8th floor is similar to yours. Why can't I come out? can you tell me it's wrong?
SELECT CONCAT(fc.city_name), SUM(fcj.area), SUM(fcj.taoshu) FROM f_chengjiao AS fcj RIGHT JOIN f_city AS fc USING (city_id) WHERE ".$sql." GROUP BY fc.city_id ORDER BY fc.city_id
Remove your where statement first
Remove your where statement first
It is actually caused by the where clause. However, if I remove the where clause, I cannot select the query results within a period of time.
It's not that you can't add where. it's your where. if there is a problem, echo your SQL statement in php and put it in mysql for check ~ Let's see where there is a problem!
So what's strange?
The f_chengjiao table does not contain the data of the newly created county. if the table is filtered by time period, it will surely be filtered out.
If you want to retain all city_name, you need to write it like this.
SELECT city_name, sum (area) as 'area ', sum (taoshu) as 'number' FROM f_city left join (select * from f_chengjiao where condition) t on f_city.city_id = t. city_id group by f_city.city_id
So what's strange?
The f_chengjiao table does not contain the data of the newly created county. if the table is filtered by time period, it will surely be filtered out.
If you want to retain all city_name, you need to write it like this.
SELECT city_name, sum (area) as 'area ', sum (taoshu) as 'number' FROM f_city left join (select * from f_chengjiao where condition) t on f_city.city_id = t. city_id group by f_city.city_id
I searched in the database and did not filter it out. you can see
What is your conditional expression?
It's not that you can't add where. it's your where. if there is a problem, echo your SQL statement in php and put it in mysql for check ~ Let's see where there is a problem!
A time period is printed:
fcj.fang_time >='2014-09-01' AND fcj.fang_time<='2014-09-13'
What is your conditional expression?
Isn't it written like this? This is the only way to write it out. Otherwise, you can check the 31st floor.
fcj.fang_time >='2014-09-01' AND fcj.fang_time<='2014-09-13'
So what's strange?
The f_chengjiao table does not contain the data of the newly created county. if the table is filtered by time period, it will surely be filtered out.
If you want to retain all city_name, you need to write it like this.
SELECT city_name, sum (area) as 'area ', sum (taoshu) as 'number' FROM f_city left join (select * from f_chengjiao where condition) t on f_city.city_id = t. city_id group by f_city.city_id
Why is there a t here? why is it used? it's all written in your way. I didn't understand what you mean by writing (select * from f_chengjiao where condition, the difference between searching all fields and directly using f_chengjiao is not clear.
You cannot understand
Select * from f_chengjiao where condition
???
1. how can I use a t?
T is just an alias associated with a temporary table. it has no special meaning. if you drop it, it will report an error.
2. Differences
1) write 1: associate the statement first and then search. in this way, records that do not meet the conditions (new counties) will not be displayed. (To understand this sentence, you can select * to see if some records do not meet your where condition)
2) Statement 2: Search (f_chengjiao) first and then associate it. this will not affect the final number of results.
You cannot understand
Select * from f_chengjiao where condition
???
I know the meaning, but why should I put where in f_chengjiao first, instead of putting it out?
1. how can I use a t?
T is just an alias associated with a temporary table. it has no special meaning. if you drop it, it will report an error.
2. Differences
1) write 1: associate the statement first and then search. in this way, records that do not meet the conditions (new counties) will not be displayed. (To understand this sentence, you can select * to see if some records do not meet your where condition)
2) Statement 2: Search (f_chengjiao) first and then associate it. this will not affect the final number of results.
Oh, I understand. But do I need to use the alias?
All as can be omitted.
All as can be omitted.
This is the case. I have to write as in my book. I also want to ask a question, that is, write according to your requirements. the f_chengjiao table has been searched twice, will the efficiency be a little lower, and there will be more data in the future?
1. this is based on your requirements, not my requirements.
2. the f_chengjiao table is only searched once. how can it be twice?
1. this is based on your requirements, not my requirements.
2. the f_chengjiao table is only searched once. how can it be twice?
The city_name, area, and taoshu values in the first search are in the f_chengjiao table.
I searched for SELECT * FROM f_chengjiao for the second time. isn't it twice?