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
Related
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 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
I have 3 tables (trx_batch, trx_doc_trx_item).
I want user to enter id(trx_batch). Then the system will auto calculate the sum of trx_item which linked to trx_doc, and trx_doc is linked to trx_batch.
My table:
CREATE TABLE IF NOT EXISTS `trx_batch` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batchDate` date NOT NULL,
`status` varchar(1) NOT NULL
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `trx_doc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idBatch` int(11) NOT NULL,
`subject` varchar(200) DEFAULT NULL,
`status` varchar(1) NOT NULL DEFAULT 'A',
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `trx_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idDoc` int(11) NOT NULL,
`amount` decimal(14,4) NOT NULL,
`amtTax` decimal(12,4) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Sample data that I have:
trx_batch
id: 1
trx_doc
id:1
idBatch: 1
trx_item:
id:1
idDoc:1
amount: 500
amtTax : 30
id:2
idDoc:1
amount:100
amtTax:20
If user enter 1 as id for trx_batch. The total should be 650.00.
How to write in mysql format? Please help me thanks!
SELECT
SUM( COALESCE( i.amount, 0 ) + COALESCE( i.amtTax, 0 ) ) AS SumAmountAndTax
FROM
trx_item AS i
INNER JOIN trx_doc AS d ON i.idDoc = doc.id
INNER JOIN trx_batch AS b ON d.idBatch = b.id
WHERE
b.id = :batchIdParameterValue
I am using MySql 5.6.10 , I want a query which has Outer query "table name" to be used in Inner Query to get all the columns of the "Table"(of Outer Queries) ,
I have two tables in a DB say test
First Table tbl_1
CREATE TABLE `tbl_1` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`log_id` int(10) DEFAULT NULL,
`new_loc_id` int(10) DEFAULT NULL,
`old_loc_id` int(10) DEFAULT NULL,
`msg` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Second Table tbl_2
CREATE TABLE `tbl_2` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(25) DEFAULT NULL,
`loc_id` int(10) DEFAULT NULL,
`parent_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I have composed something like this:
SELECT CONCAT('|QUERY|Select * from test.',tbl.table_name,'|\n' ,
'|COLUMN|',(SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ' | ') FROM information_schema.COLUMNS clm
WHERE table_schema = 'test'
AND tbl.table_name = clm.table_name),'|\n'
) AS ddl
INTO OUTFILE '/tmp/test.txt'
FROM information_schema.TABLES tbl
WHERE tbl.table_schema = 'test'
AND tbl.table_type = 'base table';
OutPut of the Query is error:
Error Code : 1054
Unknown column 'tbl.table_name' in 'where clause'
What I'm expecting is :
|QUERY|Select * from test.tbl_1|
|COLUMN|id | log_id | new_loc_id | old_loc_id | msg | id | name | loc_id | parent_id|
|QUERY|Select * from test.tbl_2|
|COLUMN|id | name| loc_id| parent_id|
The query is perfect.
except the AND AND.
WHERE table_schema = 'test' AND AND
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.