I am looking for some advice on how to build a MySQL query for the results I am looking for.
I have 2 tables tbl_features and tbl_feature_content which contain data required on my page.
tbl_features holds all the relationship info in which content is saved to the tbl_feature_content table via the parent id which is the idno of the tbl_features table.
What I am looking for is to pull a single result from the tbl_features table by its ID number and return that information in that table with also the results from the tbl_features_content table under that alias page_content.
My query to pull data from the table looks like the below.
SELECT
feature_idno AS page_idno,
feature_title AS page_title
FROM tbl_features WHERE feature_idno = 1;
How do i pull all the results from tbl_feature_content WHERE feature_content_parent = feature_idno and store it as page_content.
Table Creation scripts:
CREATE TABLE IF NOT EXISTS `tbl_features` (
`feature_idno` int(6) unsigned NOT NULL,
`feature_title` varchar(200) NOT NULL,
PRIMARY KEY (`feature_idno`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tbl_feature_content` (
`feature_content_idno` int(6) unsigned NOT NULL,
`feature_content_parent` int(6) unsigned NOT NULL,
`feature_content_description` varchar(200) NOT NULL,
PRIMARY KEY (`feature_content_idno`)
) DEFAULT CHARSET=utf8;
Sample data:
tbl_features
feature_idno feature_title
1 Feature 1
2 Feature 2
tbl_feature_content
feature_content_idno feature_content_parent feature_content_description
1 1 Something About The Feature 1
2 1 Something else About Feature 1
3 2 Something About The Feature 2
4 2 Something else About Feature 2
SQL Fiddle: http://sqlfiddle.com/#!9/3a61eb/2
EDIT.
What i am trying to achieve is the below.
Page_idno
-- 1
Page_title
-- Feature 1
Page_content
--Something About The Feature 1
--Something else About Feature 1
It sounds as though you are asking how to join tables together - like this:
SELECT
feature_idno AS page_idno,
feature_title AS page_title,
feature_content_description AS page_content
FROM tbl_features
JOIN tbl_feature_content ON feature_idno = feature_content_parent
WHERE feature_idno = 1;
SQLFiddle here: http://sqlfiddle.com/#!9/3a61eb/4
This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
Closed 2 years ago.
I am trying to execute the following query:
INSERT INTO table_listnames (name, address, tele)
VALUES ('Rupert', 'Somewhere', '022')
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name='value'
);
But this returns an error. Basically I don't want to insert a record if the 'name' field of the record already exists in another record - how to check if the new name is unique?
I'm not actually suggesting that you do this, as the UNIQUE index as suggested by Piskvor and others is a far better way to do it, but you can actually do what you were attempting:
CREATE TABLE `table_listnames` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`tele` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Insert a record:
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM `table_listnames`;
+----+--------+-----------+------+
| id | name | address | tele |
+----+--------+-----------+------+
| 1 | Rupert | Somewhere | 022 |
+----+--------+-----------+------+
Try to insert the same record again:
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
+----+--------+-----------+------+
| id | name | address | tele |
+----+--------+-----------+------+
| 1 | Rupert | Somewhere | 022 |
+----+--------+-----------+------+
Insert a different record:
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'John', 'Doe', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'John'
) LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM `table_listnames`;
+----+--------+-----------+------+
| id | name | address | tele |
+----+--------+-----------+------+
| 1 | Rupert | Somewhere | 022 |
| 2 | John | Doe | 022 |
+----+--------+-----------+------+
And so on...
Update:
To prevent #1060 - Duplicate column name error in case two values may equal, you must name the columns of the inner SELECT:
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Unknown' AS name, 'Unknown' AS address, '022' AS tele) AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM `table_listnames`;
+----+---------+-----------+------+
| id | name | address | tele |
+----+---------+-----------+------+
| 1 | Rupert | Somewhere | 022 |
| 2 | John | Doe | 022 |
| 3 | Unknown | Unknown | 022 |
+----+---------+-----------+------+
INSERT doesn't allow WHERE in the syntax.
What you can do: create a UNIQUE INDEX on the field which should be unique (name), then use either:
normal INSERT (and handle the error if the name already exists)
INSERT IGNORE (which will fail silently cause a warning (instead of error) if name already exists)
INSERT ... ON DUPLICATE KEY UPDATE (which will execute the UPDATE at the end if name already exists, see documentation)
Worked :
INSERT INTO users (full_name, login, password)
SELECT 'Mahbub Tito','tito',SHA1('12345') FROM DUAL
WHERE NOT EXISTS
(SELECT login FROM users WHERE login='tito');
MySQL provides a very cute solution :
REPLACE INTO `table` VALUES (5, 'John', 'Doe', SHA1('password'));
Very easy to use since you have declared a unique primary key (here with value 5).
INSERT IGNORE INTO `mytable`
SET `field0` = '2',
`field1` = 12345,
`field2` = 12678;
Here the mysql query, that insert records if not exist and will ignore existing similar records.
----Untested----
You can easily use the following way :
INSERT INTO ... ON DUPLICATE KEY UPDATE ...
In this way, you can insert any new raw and if you have duplicate data, replace a specific column ( The best columns are timestamps ).
For example :
CREATE TABLE IF NOT EXISTS Devices (
id INT(6) NOT NULL AUTO_INCREMENT,
unique_id VARCHAR(100) NOT NULL PRIMARY KEY,
created_at VARCHAR(100) NOT NULL,
UNIQUE KEY unique_id (unique_id),
UNIQUE KEY id (id)
)
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
INSERT INTO Devices(unique_id, time)
VALUES('$device_id', '$current_time')
ON DUPLICATE KEY UPDATE time = '$current_time';
To overcome a similar problem, I have modified the table to have a unique column. Using your example, on creation I would have something like:
name VARCHAR(20),
UNIQUE (name)
and then use the following query when inserting into it:
INSERT IGNORE INTO train
set table_listnames='Rupert'
If you really can't get a unique index on the table, you could try...
INSERT INTO table_listnames (name, address, tele)
SELECT 'Rupert', 'Somewhere', '022'
FROM some_other_table
WHERE NOT EXISTS (SELECT name
FROM table_listnames
WHERE name='Rupert')
LIMIT 1;
This query works well:
INSERT INTO `user` ( `username` , `password` )
SELECT * FROM (SELECT 'ersks', md5( 'Nepal' )) AS tmp
WHERE NOT EXISTS (SELECT `username` FROM `user` WHERE `username` = 'ersks'
AND `password` = md5( 'Nepal' )) LIMIT 1
And you can create the table using following query:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(32) NOT NULL,
`status` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Note: Create table using second query before trying to use first query.
Brian Hooper :
You almost hit the point but you have an error in your synatx. Your insert will never work. I tested on my database and here is the right answer:
INSERT INTO podatki (datum,ura,meritev1,meritev1_sunki,impulzi1,meritev2,meritev2_sunki,impulzi2)
SELECT '$datum', '$ura', '$meritve1','$sunki1','$impulzi1','$meritve2','$sunki2','$impulzi2'
FROM dual
WHERE NOT EXISTS (SELECT datum,ura
FROM podatki
WHERE datum='$datum' and ura='$ura'
I'm giving you my example of y table. Insert is almost the same like Bian Hooper wrote, except that I put the select FROM DUAL ont from other table.
Cind regards, Ivan
This is not an answer, it's just a note. The query like the one in the accepted answer does not work if the inserted values are duplicates, like here:
INSERT INTO `addr` (`email`, `name`)
SELECT * FROM (SELECT 'user#domain.tld', 'user#domain.tld') AS tmp
WHERE NOT EXISTS (
SELECT `email` FROM `addr` WHERE `email` LIKE 'user#domain.tld'
);
Error
SQL query: Copy Documentation
MySQL said: Documentation
#1060 - Duplicate column name 'user#domain.tld'
In the contrary, the query like the one from Mahbub Tito's answer works fine:
INSERT INTO `addr` (`email`, `name`)
SELECT 'user#domain.tld', 'user#domain.tld'
WHERE NOT EXISTS (
SELECT `email` FROM `addr` WHERE `email` LIKE 'user#domain.tld'
);
1 row inserted.
Tested in MariaDB
insert into customer_keyskill(customerID, keySkillID)
select 2,1 from dual
where not exists (
select customerID from customer_keyskill
where customerID = 2
and keySkillID = 1 )
You are inserting not Updating the result.
You can define the name column in primary column or set it is unique.
I had a problem, and the method Mike advised worked partly, I had an error Dublicate Column name = '0', and changed the syntax of your query as this`
$tQ = "INSERT INTO names (name_id, surname_id, sum, sum2, sum3,sum4,sum5)
SELECT '$name', '$surname', '$sum', '$sum2', '$sum3','$sum4','$sum5'
FROM DUAL
WHERE NOT EXISTS (
SELECT sum FROM names WHERE name_id = '$name'
AND surname_id = '$surname') LIMIT 1;";
The problem was with column names. sum3 was equal to sum4 and mysql throwed dublicate column names, and I wrote the code in this syntax and it worked perfectly,
I had a similar problem and I needed to insert multiple if not existing. So from the examples above I came to this combination... it's here just in case somebody would need it.
Notice:
I had to define name everywhere as MSSQL required it... MySQL works with * too.
INSERT INTO names (name)
SELECT name
FROM
(
SELECT name
FROM
(
SELECT 'Test 4' as name
) AS tmp_single
WHERE NOT EXISTS
(
SELECT name FROM names WHERE name = 'Test 4'
)
UNION ALL
SELECT name
FROM
(
SELECT 'Test 5' as name
) AS tmp_single
WHERE NOT EXISTS
(
SELECT name FROM names WHERE name = 'Test 5'
)
) tmp_all;
MySQL:
CREATE TABLE names (
OID int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (OID),
UNIQUE KEY name_UNIQUE (name)
) ENGINE=InnoDB AUTO_INCREMENT=1;
or
MSSQL:
CREATE TABLE [names] (
[OID] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (32) NOT NULL,
PRIMARY KEY CLUSTERED ([OID] ASC)
);
CREATE UNIQUE NONCLUSTERED INDEX [Index_Names_Name] ON [names]([name] ASC);
This query can be used in PHP code.
I have an ID column in this table, so I need check for duplication for all columns except this ID column:
#need to change values
SET #goodsType = 1, #sybType=5, #deviceId = asdf12345SDFasdf2345;
INSERT INTO `devices` (`goodsTypeId`, `goodsId`, `deviceId`) #need to change tablename and columnsnames
SELECT * FROM (SELECT #goodsType, #sybType, #deviceId) AS tmp
WHERE NOT EXISTS (
SELECT 'goodsTypeId' FROM `devices` #need to change tablename and columns names
WHERE `goodsTypeId` = #goodsType
AND `goodsId` = #sybType
AND `deviceId` = #deviceId
) LIMIT 1;
and now new item will be added only in case of there is not exist row with values configured in SET string
I have a basic system I am creating in which a user creates goals. When the goal is inserted, it is given a value of 0 to the status column. When the goal is completed, the value is updated to 1. So, what I am trying to do is fetch the specific user's (user with the current session) total number of ids (rows for the amount of goals) and then get the values of the status column and then count those. Once the actual status column values have been added, I want to divide that by the total number of id's. I am basically trying to figure out how to get a rate of completion.
I am not certain how I can:
-count the number of total id's for the user.
-get the values of the status column for the user and then add those.
Can anyone point me in the right direction?
Table
Create Table
goals
CREATE TABLE `goals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`title` text COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`status` int(5) NOT NULL,
`importance` int(5) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_c
$select_goals_sql = "
SELECT *
FROM goals
WHERE user_id = ?
ORDER BY id DESC
";
if ($select_goals_stmt = $con->prepare($select_goals_sql)) {
$select_goals_stmt->execute(array($user_id));
$rows = $select_goals_stmt->fetchAll(PDO::FETCH_ASSOC);
$goals = array();
foreach ($rows as $row) {
$goal_date = $row['date'];
$fixed_goal_date = fixDate($goal_date);
// the varialbe I will name it $status_completed =
$html = "";
Edit:
$goal_total_sql = "
SELECT sum(status) as sumna,
COUNT(*) as cnt
FROM goals
WHERE user_id = ?
";
if ($goal_total_stmt = $con->prepare($goal_total_sql)) {
$goal_total_stmt->execute(array($user_id));
$rows = $goal_total_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$actual_status = $row['status'];
$total_status = $row['id'];
}
}
?>
<div id="main">
<?php
echo "Actual:". $actual_status;
echo "Total". $total_status;
?>
SELECT status_id FROM goals WHERE userId = ? GROUP BY status_id should count the number of ids for each state
SELECT COUNT(*) as goal_count, status FROM goals WHERE user_id = ? GROUP BY status;
This will show you in tabular format the goal_count next to the status.
here is a sqlfiddle:
http://sqlfiddle.com/#!9/1bb23
so using a modified version of your create table (You should be using Innodb and I ommited the Collac for simplicity)
your result should be like this:
+------------+--------+
| goal_count | status |
+------------+--------+
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
+------------+--------+
I's hard to tell what you need here because the question title contradicts with body and both with code provided. But as far as I can tell
SELECT sum(status) as summa, COUNT(*) as cnt FROM ...
My code is like this:
$zone = $_POST['zone'];
$state = $_POST['state'];
$sql = "Select zone_id from tbl_zone where zone_name = $zone";
$query = mysql_query($sql);
I am selecting zone name which is fetched from database and listed in drop down list.
Now i want to get id of zone name to store in other table.
In where clause zone_name is blank.
What changes should be done in above code?
Thanks in advance..
Try this one..
$sql = "Select zone_id from tbl_zone where zone_name = '$zone'";
I suggest you to design a proper database. Like your question is somewhat confusing. Let me tell you the process. First design 2 tables with 2 columns each. Let us assume table 1 as state and table 2 as city. state table contains 2 columns namely zone_id and zone_state. city table contains 2 columns namely zone_id and zone_city. make state table with zone_id as primary key with auto_increment field and give each id with unique state like zone_id 1 as AP, zone_id 2 as Tamilnadu, zone_id 3 as Karnataka, zone_id 4 as Kerala, zone_id 5 as Maharashtra etc...now for the city table add zone_id with repeated values like zone_id 1 as Hyderabad,zone_id 1 as Vijayawada,zone_id 1 as Vizag, etc..zone_id 2 as Chennai,zone_id 2 as Coimbatore, etc...i hope you got my point...If you design like this then retrieval of results will be easy for you.
see this code:
CREATE TABLE IF NOT EXISTS `state` (
`zone_id` int(2) NOT NULL AUTO_INCREMENT,
`zone_state` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`zone_id`)
) ENGINE=MyISAM;
CREATE TABLE IF NOT EXISTS `city` (
`zone_id` int(2) NOT NULL DEFAULT '0',
`zone_city` varchar(25) NOT NULL DEFAULT ''
) ENGINE=MyISAM;