UPDATE
Sometime, when a family is being inactivated from a system, it may contain more than 1 individual. In my case show at the sql fiddle, the family with household_id=12 has 3 individuals.
I need to insert the data of these 3 individuals as the same from indiviudal table to individual_history table and just changing the ind_action field into the following message HH has been inactivated.
Here is a sample data:
| individual_id | household_id | family_relation_id | marital_status_id | ind_lmms_id | ind_un_id | head_of_hh | ind_first_name_ar | ind_last_name_ar | ind_first_name_en | ind_last_name_en | ind_gender | dob | ind_status | ind_date_added | user_id | system_date |
|---------------|--------------|--------------------|-------------------|-------------|-----------|------------|-------------------|------------------|-------------------|------------------|------------|------------|------------|----------------------|---------|----------------------|
| 1 | 12 | 3 | 1 | 321 | (null) | no | u | x | (null) | (null) | Male | 2012-01-01 | Active | 2018-07-19T00:00:00Z | 1 | 2018-07-19T00:00:00Z |
| 2 | 12 | 1 | 2 | 123 | (null) | no | x | y | (null) | (null) | Female | 1998-03-05 | Active | 2015-03-05T00:00:00Z | 1 | 2015-03-05T00:00:00Z |
| 3 | 12 | 3 | 1 | 1234 | (null) | no | x | z | (null) | (null) | Female | 2004-04-05 | Active | 2018-04-11T00:00:00Z | 1 | 2018-04-11T00:00:00Z |
All 3 fields should be inserted to the table individual_history and ind_action is set to the note I added above.
I need to insert into a table called individual_history values of a SELECT query from table individual.
Here is the query:
INSERT INTO individual_history
(individual_id,
household_id,
family_relation_id_history,
marital_status_id_history,
ind_lmms_id_history,
ind_un_id_history,
head_of_hh_history,
ind_first_name_ar_history,
ind_last_name_ar_history,
ind_first_name_en_history,
ind_last_name_en_history,
ind_gender_history,
dob_history,
ind_status_history,
ind_action,
ind_date_changed,
user_id,
system_date)
VALUES ((SELECT i.individual_id,
i.household_id,
i.family_relation_id,
i.marital_status_id,
i.ind_lmms_id,
i.ind_un_id,
i.head_of_hh,
i.ind_first_name_ar,
i.ind_last_name_ar,
i.ind_first_name_en,
i.ind_last_name_en,
i.ind_gender,
i.dob,
i.ind_status
FROM individual i
WHERE i.household_id = :hid),
'HH Status Changed to inactive',
(SELECT i.ind_date_added,
i.user_id
FROM individual i
WHERE i.household_id = :hid),
:systemDate)
As you can see from the query, I am splitting the SELECT statement into 2 parts, as I want to insert a specific ind_action message, then I will continue by getting the other 2 fields date added and user_id.
The systemDate is the just the now() function result.
I tried to run this query using 12 as hid and I received the following error:
1136 - Column count doesn't match value count at row 1
After doing few searches, I found that I should add parenthesis for each of the values. So I changed the query to:
INSERT INTO individual_history
(individual_id,
household_id,
family_relation_id_history,
marital_status_id_history,
ind_lmms_id_history,
ind_un_id_history,
head_of_hh_history,
ind_first_name_ar_history,
ind_last_name_ar_history,
ind_first_name_en_history,
ind_last_name_en_history,
ind_gender_history,
dob_history,
ind_status_history,
ind_action,
ind_date_changed,
user_id,
system_date)
VALUES ((SELECT i.individual_id,
i.household_id,
i.family_relation_id,
i.marital_status_id,
i.ind_lmms_id,
i.ind_un_id,
i.head_of_hh,
i.ind_first_name_ar,
i.ind_last_name_ar,
i.ind_first_name_en,
i.ind_last_name_en,
i.ind_gender,
i.dob,
i.ind_status
FROM individual i
WHERE i.household_id = 12),
( 'HH Status Changed to inactive' ),
(SELECT i.ind_date_added,
i.user_id
FROM individual i
WHERE i.household_id = 12),
( NOW() ))
But still got the same error.
I tried to count the number of fields I am inserting compared to the ones I am selecting, and they are the same (18 fields).
UPDATE
I changed the query by removing the VALUES clause:
INSERT INTO individual_history
(
individual_id,
household_id,
family_relation_id_history,
marital_status_id_history,
ind_lmms_id_history,
ind_un_id_history,
head_of_hh_history,
ind_first_name_ar_history,
ind_last_name_ar_history,
ind_first_name_en_history,
ind_last_name_en_history,
ind_gender_history,
dob_history,
ind_status_history,
ind_action,
ind_date_changed,
user_id,
system_date
)
SELECT i.individual_id,
i.household_id,
i.family_relation_id,
i.marital_status_id,
i.ind_lmms_id,
i.ind_un_id,
i.head_of_hh,
i.ind_first_name_ar,
i.ind_last_name_ar,
i.ind_first_name_en,
i.ind_last_name_en,
i.ind_gender,
i.dob,
i.ind_status
FROM individual i
WHERE i.household_id=12,
'HH Status Changed to inactive',
(
SELECT i.ind_date_added,
i.user_id
FROM individual i
WHERE i.household_id=12),
now()
And I got the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '
'HH Status Changed to inactive',
' at line 10
Please note that the datatype of fields are exactly the same in both tables, and individual_history table contain an auto-increment primary key.
HERE IS AN SQL FIDDLE to check with sample data.
You don't need two SELECTs for what you're trying to do. If you want to use some specific value for ind_action, simply replace it in your select, same as you did with the now() function:
INSERT INTO targetTable (col1, col2, col3, col4, colTime)
SELECT colA, colB, 'my specific string', colD, now()
FROM sourceTable WHERE colA = 12;
Here, col3 gets the string, colTime the now().
#Marting Hennings, I am a bit too late ... but this query should work:
INSERT INTO individual_history
(individual_id,
household_id,
family_relation_id_history,
marital_status_id_history,
ind_lmms_id_history,
ind_un_id_history,
head_of_hh_history,
ind_first_name_ar_history,
ind_last_name_ar_history,
ind_first_name_en_history,
ind_last_name_en_history,
ind_gender_history,
dob_history,
ind_status_history,
ind_action,
ind_date_changed,
user_id,
system_date)
SELECT individual_id,
household_id,
family_relation_id,
marital_status_id,
ind_lmms_id,
ind_un_id,
head_of_hh,
ind_first_name_ar,
ind_last_name_ar,
ind_first_name_en,
ind_last_name_en,
ind_gender,
dob,
ind_status,
'HH Status Changed to inactive',
ind_date_added,
user_id,
now()
FROM individual
WHERE individual.household_id = 12
I have the following table:
Field | Type | Null | Key | Default | Extra |
+-----------------+----------------------+------+-----+---------+-------+
| SMILES | char(200) | NO | PRI | | |
| ConfRank | smallint(5) unsigned | NO | | NULL | |
| CompTime | double | YES | | NULL | |
I want to get the total CompTime for a specific SMILES (meaning Comptime are added for al confrank of a specific smiles).
I tried the following:
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies GROUP BY ConfRank WHERE SMILES='C';
And received this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE SMILES='C'' at line 1
I also tried:
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies GROUP BY ConfRank WHERE SMILES='C' GROUPBY ConfRank ;
And received another error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE SMILES='C' GROUPBY ConfRank' at line 1
What would be the correct syntax?
try the following
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies WHERE SMILES='C' GROUP BY ConfRank;
Your group by clause should appear after where clause.
so I used
SELECT SMILES, (SUM(CompTime)) From GeoAndEnergies WHERE Method ='PM6' GROUP BY SMILES INTO OUTFILE "/var/lib/mysql-files/data_PM6_timesum" ;
and
mysql> SELECT SMILES, (SUM(CompTime)) From GeoAndEnergies WHERE Method ='PM6' GROUP BY SMILES INTO OUTFILE "/var/lib/mysql-files/data_PM6_time" ;
and SELECT SMILES, NRotBond , NHeavyAtom From GeoAndEnergies WHERE Method ='PM6' AND ConfRank =1 INTO OUTFILE "/var/lib/mysql-files//data_PM6_rotheavy" ;
to create data_PM6_rotheavy and data_PM6_time.
By catting the 2 you should have what you want.
Your GROUP BY should follow the WHERE clause. Update as follows:
SELECT `SMILES`,
SUM(`CompTime`)
FROM `GeoAndEnergies`
WHERE `SMILES` = 'C'
GROUP BY `ConfRank`;
This question already has answers here:
MySQL Syntax error message "Operand should contain 1 column(s)"
(5 answers)
Closed 8 years ago.
I have a table, exposure, which looks like this:
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| src | varchar(20) | NO | | NULL | |
| instrument | varchar(10) | NO | | NULL | |
| result | decimal(12,3) | NO | | NULL | |
| instant | bigint(20) | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
This table is used to store historical exposure values, and is expected to contain the occasional gap. I'm trying to select, for a given src and instant, all (instrument, result, instant)'s either at that target instant or at the largest instant smaller than the target instant. The following SELECT statement does exactly that.
SELECT * FROM exposure AS e
WHERE e.instant = (SELECT MAX(instant) FROM exposure
WHERE instant <= 1396985195077
AND src = 'testSrc')
AND e.src = 'testSrc';
However, when I try to specify the columns I wish to select as in the following query, MySQL responds with ERROR 1241 (21000): Operand should contain 1 column(s). I suspect it is confusing the inner and outer queries.
SELECT (e.instrument, e.result, e.instant) FROM exposure AS e
WHERE e.instant = (SELECT MAX(instant) FROM exposure
WHERE instant <= 1396985195077
AND src = 'testSrc')
AND e.src = 'testSrc';
My MySQL version string is: Server version: 5.5.34-0ubuntu0.13.04.1 (Ubuntu).
Change your SELECT (e.instrument, e.result, e.instant) to
SELECT e.instrument, e.result, e.instant
FROM ..
using columns in () mysql identifies it as any operation to perform thus there are multiple columns you get an error that there should be one column if you still wanna use parenthesis you can select your columns as
SELECT (e.instrument), (e.result), (e.instant)
FROM ..
I have an issue with a mysql subselect.
**token table:**
id | token | articles
1 | 12345 | 7,6
2 | 45saf | 6,7,8
**items table:**
id | name | filename
6 | Some brilliant name | /test/something_useful.mp3
7 | homer simpson | /test/good-voice.mp3
**query:**
SELECT items.`filename`,items.`name` FROM rm_shop items WHERE items.`id` IN ( SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')
I only get one of the two files (with the id 7 that is). What am I missing here?
For a column with concatenated data (like your "articles" column), you can not use MySQL IN() Function. Instead use the string function FIND_IN_SET() to query such values. In your case:
SELECT items.`filename`,items.`name` FROM rm_shop items
WHERE FIND_IN_SET(items.`id`,
(SELECT token.`articles` FROM rm_token token WHERE token.`token` = 'token')) > 0
A working sqlfiddle: http://sqlfiddle.com/#!2/796998/3/0