I am trying to select some fields from the table with customized values, When I run the select query its showing first two selected columns irrespective of the order in select query (like country,name,slug,area_slug or country,slug,area_slug,name).
please find the table schema and the query below.
TABLE:
CREATE TABLE `destination` (
`code` varchar(5) NOT NULL,
`country` varchar(2) NOT NULL,
`region` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`parent` varchar(5) DEFAULT NULL,
`latitude` double(30,15) NOT NULL,
`longitude` double(30,15) NOT NULL,
`updated` varchar(30) NOT NULL DEFAULT '0000-00-00T00:00:00.000000Z'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
QUERY:
SELECT 'ae' as country,
name,
replace(lcase(name), ' ', '-') as slug,
CONCAT(replace(lcase(name), ' ', '-'),'-united-arab-emirates') as area_slug
FROM `destination`
WHERE code IN ("18ed5","18ed6","18ed7","18ed8","18ed9","18eda","18edb")
According to the error message in the screenshot provided you do not have a unique column in the query, and that is true. There does not even seem to be one in the table. I suggest you add an auto incrementing ID into your table, then include that column in your query.
for example:
DROP TABLE IF EXISTS `destination`;
CREATE TABLE `destination` (
`id` mediumint(8) unsigned NOT NULL auto_increment primary key,
`code` varchar(5) NOT NULL,
`country` varchar(2) NOT NULL,
`region` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`parent` varchar(5) DEFAULT NULL,
`latitude` double(30,15) NOT NULL,
`longitude` double(30,15) NOT NULL,
`updated` varchar(30) NOT NULL DEFAULT '0000-00-00T00:00:00.000000Z'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
INSERT INTO `destination` (code,country,region,name,parent,latitude,longitude,updated)
VALUES ('18ed5','1','1','1','1','1',1,2);
SELECT ID
, 'ae' AS country
, name
, REPLACE(Lcase(name), ' ', '-') AS slug
, Concat(REPLACE(Lcase(name), ' ', '-'), '-united-arab-emirates') AS area_slug
FROM `destination`
WHERE code IN ( '18ed5', '18ed6', '18ed7', '18ed8','18ed9', '18eda', '18edb' )
result
| ID | country | name | slug | area_slug |
|----|---------|------|------|------------------------|
| 1 | ae | 1 | 1 | 1-united-arab-emirates |
see: http://rextester.com/LER16583
Related
I want to add and calculate using data from 2 tables.
The results I want are as follows.
However, if I send a query as follows:
SELECT COUNT(IF(treatment_fees_check_division = 'test',
treatment_fees_check_division, NULL)) AS COUNT, SUM(CASE WHEN
treatment_fees_check_division = 'test' THEN treatment_fees_difference
END) + SUM(advenced_amount) AS if_treatment_fees_check_division,
SUM(advenced_amount) AS sum_init_amount FROM ( SELECT
treatment_fees_check_division, treatment_fees_difference,
advenced_amount FROM hospital_payment_data, advenced_payment WHERE
hospital_payment_data.id = advenced_payment.chart_num ) AS a
These results occur.
create table of hospital_payment_data and data
CREATE TABLE `hospital_payment_data` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` TIMESTAMP NOT NULL DEFAULT current_timestamp(),
`chart_num` INT(11) NOT NULL ,
`chart_name` VARCHAR(50) NOT NULL,
`visit` DATE NOT NULL,
`non_payment_sales` VARCHAR(50) NOT NULL,
`total_medical_bills` VARCHAR(50) NOT NULL,
`total_amount` VARCHAR(50) NOT NULL,
`amount_asked` VARCHAR(50) NOT NULL,
`medical_bills_payment` VARCHAR(50) NOT NULL,
`personal_liability_amount` VARCHAR(50) NOT NULL,
`non_payment` VARCHAR(50) NOT NULL,
`insurance_division` VARCHAR(50) NOT NULL,
`division` VARCHAR(50) NOT NULL,
`cash_amount_received` VARCHAR(50) NOT NULL,
`cash_receipt` VARCHAR(50) NOT NULL,
`cash_receipt_non_payment` VARCHAR(50) NOT NULL,
`cash_receipt_payment` VARCHAR(50) NOT NULL,
`card_amount_received` VARCHAR(50) NOT NULL,
`card_non_payment` VARCHAR(50) NOT NULL,
`card_payment` VARCHAR(50) NOT NULL,
`medical_bills_check` VARCHAR(50) NOT NULL,
`medical_bills_check_modify` VARCHAR(50) NOT NULL DEFAULT 'N',
`treatment_fees_difference` VARCHAR(50) NOT NULL,
`init_amount` VARCHAR(50) NOT NULL DEFAULT '0',
`treatment_fees_check_division` VARCHAR(50) NOT NULL DEFAULT '-',
`treatment_fees_check` VARCHAR(50) NOT NULL,
`treatment_fees_check_modify` VARCHAR(50) NOT NULL DEFAULT 'N',
`treatment_fees_check_memo` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='euckr_korean_ci'
ENGINE=InnoDB
AUTO_INCREMENT=18
;
create table of advenced_payment and data
CREATE TABLE `advenced_payment` (`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` TIMESTAMP NULL DEFAULT NULL,
`chart_num` VARCHAR(50) NULL DEFAULT NULL,
`chart_name` VARCHAR(50) NULL DEFAULT NULL,
`advenced_amount` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `chart_num` (`chart_num`)) COLLATE='euckr_korean_ci' ENGINE=InnoDB AUTO_INCREMENT=2 ;
How do fix my query??
You have numerous issues with you query. This won't fix your problems, but I recommend writing it as:
SELECT SUM(hpd.treatment_fees_check_division = 'test') AS COUNT,
(SUM(CASE WHEN hpd.treatment_fees_check_division = 'test' THEN hpd.treatment_fees_difference END) +
SUM(advenced_amount)
) AS if_treatment_fees_check_division,
SUM(advenced_amount) AS sum_init_amount
FROM hospital_payment_data hpd JOIN
advanced_payment ap
ON hpd.id = ap.chart_num;
This simplifies and fixes some issues:
The JOIN uses proper, explicit, standard, readable syntax. Never use commas in the FROM clause.
The subquery is utterly unnecessary and probably adversely affects performance.
All tables have table aliases.
All column references are qualified.
The condition count is much simplified using sum(<boolean>).
I note that treatment_fees_difference is a string, yet you are using SUM(). That is really, really bad.
In any case, your problem, is that one of the tables has multiple rows. It is hard to know which one but my guess is advanced_payment. The solution is to preaggregate before the JOIN:
SELECT SUM(hpd.treatment_fees_check_division = 'test') AS COUNT,
(SUM(CASE WHEN hpd.treatment_fees_check_division = 'test' THEN hpd.treatment_fees_difference END) +
SUM(ap.sum_init_amount)
) AS if_treatment_fees_check_division,
SUM(ap.sum_init_amount) AS sum_init_amount
FROM hospital_payment_data hpd JOIN
(SELECT ap.chart_num, COUNT(*) as cnt, SUM(advenced_amount) as sum_init_amount
FROM advanced_payment ap
GROUP BY ap.chart_num
) ap
ON hpd.id = ap.chart_num;
I am hoping someone can advise on what is wrong.
SELECT sms.id FROM sms INNER JOIN screens ON sms.screen_id = screens.id WHERE screens.experience_id = '108';
AND
SELECT id FROM sms
WHERE screen_id IN (SELECT id FROM screens WHERE experience_id = 108 )
... returns an empty row, but there are rows that should be returned.
To clarify...
SELECT id FROM sms
... returns all rows in SMS
SELECT id FROM sms
WHERE screen_id IN (SELECT id FROM screens)
... returns all rows in SMS and
SELECT id FROM screens WHERE experience_id = 108
... returns all rows from screens with that id.
SQL to make the tables...
CREATE TABLE IF NOT EXISTS `screens` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`device_id` int(11) NOT NULL,
`type` varchar(32) NOT NULL,
`description` text NOT NULL,
`position` int(11) NOT NULL,
`experience_id` int(11) NOT NULL,
`image` varchar(512) NOT NULL,
`url` varchar(512) NOT NULL,
`persona_id` int(11) NOT NULL,
`socialPost` text NOT NULL,
`socialLinkHeadline` varchar(512) NOT NULL,
`socialLinkDescription` text NOT NULL,
`socialLink` varchar(512) NOT NULL,
`socialAction` varchar(64) NOT NULL,
`bgcolor` varchar(16) NOT NULL DEFAULT '#fff',
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=472 ;
CREATE TABLE IF NOT EXISTS `sms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` varchar(16) NOT NULL,
`content` text NOT NULL,
`position` int(11) NOT NULL,
`screen_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
A GIF of me running these queries to verify is here: https://ibb.co/gzPFsd
I am at a loss and can see that people can't replicate, thanks!
I don't get what's wrong in your query but you can do that using join as well.
SELECT sms.id FROM sms INNER JOIN screens ON sms.screen_id = screens.id WHERE screens.experience_id = '108';
Your query works fine , here's the proof.
DROP TABLE IF EXISTS SMS,SCREENS;
CREATE TABLE SMS(ID INT, SCREEN_ID INT);
CREATE TABLE SCREENS(ID INT, EXPERIENCE_ID INT);
INSERT INTO SMS VALUES (1,1),(2,2);
INSERT INTO SCREENS VALUES(1,108),(2,100);
SELECT id FROM sms
WHERE screen_id IN (SELECT id FROM screens WHERE experience_id = 108 );
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
Assuming that my data model and yours are the same.
i have problem with select.
I managed to write a query that was doing something similar but I was able to pull data after only one I need to group as a team but with two names
I have a problem to write one question, I need to pick something about this style
team_id | player1 | player2 | position | category
1 | John Newman | Andy Roddick | 1 | 1
2 | Roger Federer | Rafael Nadal | 1 | 1
I have two tables/ one contains players and second contains attendance in team/
CREATE TABLE IF NOT EXISTS `atendance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`player1` int(11) NOT NULL,
`player2` int(11) NOT NULL,
`position` int(11) NOT NULL,
`category` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=22 ;
CREATE TABLE IF NOT EXISTS `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(12) COLLATE utf8_czech_ci NOT NULL,
`surname` varchar(24) COLLATE utf8_czech_ci NOT NULL,
`datum_n` date NOT NULL,
`klub` varchar(60) COLLATE utf8_czech_ci NOT NULL,
`tel` varchar(12) COLLATE utf8_czech_ci NOT NULL,
`mail` varchar(32) COLLATE utf8_czech_ci NOT NULL,
`cas` text COLLATE utf8_czech_ci NOT NULL,
`foto` text COLLATE utf8_czech_ci NOT NULL,
`pass` text COLLATE utf8_czech_ci NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '0',
`valid_from` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`valid_until` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`is_admin` tinyint(1) NOT NULL DEFAULT '0',
`pohlavie` varchar(6) COLLATE utf8_czech_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=25 ;
I think you need to use JOIN particularly LEFT JOIN, like:
SELECT t.id, p1.name, p2.name, t.position, t.category
FROM atendance t
LEFT JOIN players p1
ON t.player1 = p1.id
LEFT JOIN players p2
ON t.player2 = p2.id
As much as i understood, all the required data is in first table. You just need to fire,
SELECT ID, PLAYER1, PLAYER2, POSITION, CATEGORY FROM ATENDANCE
Please note, instead of CREATE commands provide the output result of that table, as you want SELECT statement to be built. It makes easier to understand.
I have two tables, dma_projects and projectsteps:
dma_projects has the following fields:
projectID
projectName
projectInstructions
CREATE TABLE IF NOT EXISTS `dma_projects` (
`projectID` int(11) NOT NULL DEFAULT '0',
`projectName` varchar(100) DEFAULT NULL,
`projectDescription` text,
`projectImage` varchar(255) DEFAULT NULL,
`projectThumb` varchar(255) DEFAULT NULL,
`projectCategory` varchar(50) DEFAULT NULL,
`projectTheme` varchar(50) DEFAULT NULL,
`projectInstructions` text,
`projectAuthorID` int(11) DEFAULT NULL,
`projectViews` int(11) DEFAULT NULL,
`projectDifficulty` varchar(20) DEFAULT NULL,
`projectTimeNeeded` varchar(40) DEFAULT NULL,
`projectDateAdded` int(11) DEFAULT NULL,
`projectStatus` varchar(20) DEFAULT NULL,
`projectVisible` varchar(1) DEFAULT NULL,
PRIMARY KEY (`projectID`),
KEY `user_id` (`projectAuthorID`),
KEY `date` (`projectDateAdded`),
FULLTEXT KEY `image` (`projectImage`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
projectsteps has:
stepID
stepno
stepdesc
project_id
CREATE TABLE IF NOT EXISTS `projectsteps` (
`projectStep_id` int(11) NOT NULL AUTO_INCREMENT,
`stepno` int(11) DEFAULT '0',
`stepdesc` text CHARACTER SET utf8 COLLATE utf8_bin,
`project_id` int(11) NOT NULL,
PRIMARY KEY (`projectStep_id`)
)
I want to update dma_projects.projectInstructions with the values of any rows that are found in the projectsteps table that have the same projectID.
I.e.
projectID 1 in dma_projects has 5 records in projectsteps, the stepdesc of those five records should be joined (separated by a ) and then updated to the projectInstructions field of the dma_projects table.
I'm scratching my head as how to write the query. Here is where I am so far but I can't get it working. The error it says is:
Unknown column 'projectsteps.stepno' in 'field list'
Here is the query:
UPDATE `dma_projects` t1
SET t1.`projectInstructions` =
(
SELECT
`projectsteps`.`stepno`,
group_concat(`projectsteps`.`stepdesc` separator '<br/>')
FROM `projectsteps`
AS somevar
INNER JOIN `projectsteps` t2
ON t1.projectID=t2.project_id
ORDER BY t2.stepno ASC
)
UPDATED
UPDATE dma_projects p JOIN
(
SELECT project_id, GROUP_CONCAT(CONCAT('<li>', stepdesc, '</li>') SEPARATOR '') instructions
FROM
(
SELECT project_id, stepdesc
FROM projectsteps
ORDER BY project_id, stepdesc
) a
GROUP BY project_id
) d ON d.project_id = p.projectid
SET p.projectInstructions = d.instructions
Sample output:
| PROJECTID | PROJECTNAME | PROJECTINSTRUCTIONS |
------------------------------------------------------------------------------------------
| 1 | project1 | <li>step11</li><li>step12</li><li>step13</li><li>step14</li> |
| 2 | project1 | <li>step21</li><li>step22</li><li>step23</li> |
Here is SQLFiddle demo
SELECT SUBSTRING(m.own,3,4) as c , (select amphur.AMPHUR_NAME where c = SUBSTRING(m.own,3,4) ),
COUNT(* ) AS cnt
FROM MEMBER AS m
GROUP BY SUBSTRING(m.own,3,4)
order by cnt desc
sql statement mysql
what wrong with code below when i fill
(select amphur.AMPHUR_NAME where c = SUBSTRING(m.own,3,4) )
it error
CREATE TABLE IF NOT EXISTS `member` (
`idmember` int(11) NOT NULL AUTO_INCREMENT,
`own` varchar(255) DEFAULT NULL,
`Sname` varchar(255) DEFAULT NULL,
`Ssurname` varchar(255) DEFAULT NULL,
`Sex` enum('¿','¿') NOT NULL,
`Hno` varchar(255) DEFAULT NULL,
`Moo` varchar(255) DEFAULT NULL,
`tambol` varchar(200) NOT NULL,
`dateofbirth` date DEFAULT NULL,
`migratedate` date DEFAULT NULL,
`status` enum('5','4','3','2','1') DEFAULT '5',
`Unit` int(4) DEFAULT NULL,
`staff1` int(11) DEFAULT NULL,
`staff2` int(11) DEFAULT NULL,
`fathercode` varchar(30) NOT NULL,
`mathercode` varchar(30) NOT NULL,
PRIMARY KEY (`idmember`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8994 ;
CREATE TABLE IF NOT EXISTS `amphur` (
`AMPHUR_ID` int(5) NOT NULL AUTO_INCREMENT,
`AMPHUR_CODE` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`AMPHUR_NAME` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`GEO_ID` int(5) NOT NULL DEFAULT '0',
`PROVINCE_ID` int(5) NOT NULL DEFAULT '0',
`province_name` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`AMPHUR_ID`),
KEY `province_name` (`province_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=999 ;
Your subquery is missing a From clause:
SELECT SUBSTRING(m.own,3,4) as c
, (select amphur.AMPHUR_NAME
From amphur
Where ??? = SUBSTRING(m.own,3,4) )
, COUNT(* ) AS cnt
FROM MEMBER AS m
However, how does the amphur table relate to the member table?
You cannot use aliases in the same level.
Even if you could, you are filtering on non-correlated columns in your subquery: the subquery would just return the record from amphur if there is one record, or an error if there are more.
Could you please provide some sample data and the desired recordset?
there is no "FROM" clause in your select Amphur.amphur_name