I 'm stuck on a very basic problem, I want to skip the row which has duplicate values over three columns.
Table feeds
id,type,user_id,friend_id,date
1, 'friend', 4 , 5 , 5/5/2010
2, 'friend', 5 , 4 , 5/5/2010
Now this is how the data is saved (I can't change the saving module)
since both have same thing, so I want to pick them only as a 1 row not 2.
I don't want to validate and remove it at PHP end, b/c if I'll do at PHP the pagination would be disturb
Edit:
Table Structure
create table `feed` (
`id` double ,
`type` blob ,
`user_id` double ,
`type_id` double ,
`date` datetime
);
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('78','friends','1314','1313','2012-09-03 19:48:14');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('79','friends','1313','1314','2012-09-03 19:48:14');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('80','friends','1314','1312','2012-09-03 19:49:07');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('82','friends','1313','1312','2012-09-03 19:49:09');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('84','friends','1315','1312','2012-09-03 19:49:24');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('86','friends','1315','1313','2012-09-03 19:49:33');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('87','friends','1313','1315','2012-09-03 19:49:33');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('97','friends','1317','1312','2012-09-03 19:55:06');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('99','friends','1313','1317','2012-09-03 19:56:01');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('100','friends','1317','1313','2012-09-03 19:56:01');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('101','friends','1315','1317','2012-09-03 19:56:58');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('102','friends','1317','1315','2012-09-03 19:56:58');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('104','following','1313','1193','2012-09-03 19:59:39');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('105','following','1313','1308','2012-09-03 19:59:51');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('106','following','1313','1098','2012-09-03 19:59:58');
And here is the ultimate solution! If the same friendship pair (reversed) exists it only takes the one where user_id>friend_id.
SELECT DISTINCT type, user_id, friend_id, date
FROM table t1
WHERE t1.user_id > t1.friend_id
OR NOT EXISTS (
SELECT * FROM table t2
WHERE t1.type=t2.type AND t1.date=t2.date
AND t2.user_id = t1.friend_id AND t2.friend_id = t1.user_id
)
The function you are looking for is DISTINCT(). That allows you to group the table by that column and remove duplicates.
Just make sure in your select statement that you also select the other field columns as well:
SELECT id, DISTINCT(type), user_id, friend_id, date FROM TABLENAME
You could perform a JOIN on the table itself, which should do what you are looking for.
Something like this might do the trick:
SELECT * FROM tableName a
JOIN tableName b ON a.user_id = b.friend_id
You should get rid of those duplicate records by running:
DELETE FROM table t1 WHERE
EXISTS (
SELECT * FROM table t2
WHERE t1.type=t2.type AND t1.date=t2.date
AND t2.user_id = t1.friend_id AND t2.friend_id = t1.user_id
AND t1.user_id > t2.user_id
)
Then you can get distinct friendship records with:
SELECT DISTINCT type, user_id, friend_id, date FROM table
Also, you can write a DB trigger that fires on INSERT, that checks if a duplicate (with reversed user_id and friend_id) already exists. If exists it does ROLLBACK, else allow INSERT.
Related
I just want to ask. what is the problem in here I just want to add some value to the the columns and get other value to the other table. I hope you can help me. I only have the problem in the first 4 value.
Error: INSERT INTO `orders` (`address`, `payment_method`, `contactnumber`, `order_status`, `cart_id`, `user_id`, `item_id`) VALUES ('Bulacan', '','0912312332', 'Pending' ,(SELECT `cart_id`,`user_id`,`item_id` FROM `cart`))
Operand should contain 1 column(s)
$sql = "INSERT INTO `orders` (`address`, `payment_method`, `contactnumber`, `order_status``cart_id`, `user_id`, `item_id`)
VALUES ('$address', '$payment', '$cnumber', '$status'(SELECT `cart_id`,`user_id`,`item_id` FROM `cart`) )";
-Thankyou.
INSERT INTO `orders` (`address`, `payment_method`, `contactnumber`,
`order_status`, `cart_id`, `user_id`, `item_id`)
SELECT 'Bulacan', '','0912312332', 'Pending', `cart_id`,`user_id`,`item_id`
FROM `cart`
WHERE { ??? }
I have a dictionary database with 950 rows of data. I would like to show the words list in alphabetical order in alphabetical table wise.
For an example table a shows first (5) words of data's starts from A:
A
-------------
Abacus
Abelian group
Abscissa
Absolute Value
Abstract Number
B
---------------
Bar Graph
Base
Base Depth of the Triangular Prism
Base of the Triangular Prism
Basic arithmetic operations
.
.
.
Z
---------------
Z-Intercept
Zero
Zero Divisors
Zero Element
Zone
every 5 data's fetch table should stop the current letter and jump to next letter.
I don't know how to do this.
My Table structure is
-------------------------
Name | Type
-------------------------
Id | int(3)
Word | varchar(45)
-------------------------
You should extract first letter with left
select left(word, 1) from table;
function and use huge union all query. details is described with examples in here.
I tried below query which has limit for now of 3.
In short what we have to do is - group by sorted words and limit to n
Created table as word_table
CREATE TABLE `offers`.`word_table` ( `Id` INT(3) NOT NULL , `Word`
VARCHAR(45) NOT NULL ) ENGINE = InnoDB;
Inserted your record in table
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('1', 'Abacus');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('2', 'Abelian group');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('3', 'Abscissa');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('4', 'Absolute Value');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('5', 'Abstract Number');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('6', 'Bar Graph');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('7', 'Base');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('8', 'Base Depth of the Triangular Prism');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('9', 'Base of the Triangular Prism');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('10', 'Basic arithmetic operations');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('11', 'Z-Intercept');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('12', 'Zero');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('13', 'Zero Divisors');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('14', 'Zero Element');
INSERT INTO `word_table` (`Id`, `Word`) VALUES ('15', 'Zone');
Query written for required output
SELECT x.*
FROM (SELECT t.*,
CASE
WHEN #category != substring(t.Word,1,1) THEN #rownum := 1
ELSE #rownum := #rownum + 1
END AS rank,
#category := substring(t.Word,1,1) AS var_category
FROM word_table t
JOIN (SELECT #rownum := NULL, #category := '') r
ORDER BY t.Word) x
WHERE x.rank <= 3
ORDER BY `x`.`var_category` ASC
OUTPUT
Hope this helps you!!!
You can use a single query to get the result of both the distinct first character and all the records with that letter as starting letter by using this
SELECT lower(left(filed_name,1)) as letter, group_concat(filed_name) as all_records FROM `table_name` where lower(left(filed_name,1)) in(SELECT DISTINCT LEFT(filed_name, 1) FROM table_name order by filed_name) group by lower(left(filed_name,1)) ORDER BY filed_name ASC
Sample code:
echo $mylinks;
Outputted echo:
http://www.google1.com|http://www.google2.com
I want to insert each of these into a database table.
Here's what I was trying to use:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, '".$mylinks."', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
The problem is, it's inserting into the database like this basically:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com|http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
Whereas I want each URL to be entered into its own row like this:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
URLs are separated by a pipe (|) - there's normally more than one URL that's submitted, but sometimes there's only a single URL - in case that makes a difference.
So how do I submit the URLs into my database so each is on a separate row?
mysql will not split that for you, you need to split it yourself.
$stmt = $pdo->prepare("INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL,:link, '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP)");
foreach (explode("|", $mylinks) as $link) {
$stmt->execute(array(":link" => $link))
}
You can convert this string to an array with php explode function and after that insert data like this:
$array = explode('|', $mylinks);
foreach($array as $key => $value)
//Insert $value into table
This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 7 years ago.
This is what i want to do but with lots of records. When i try this i get this kind of error:the error message i get
this is my currant php and when i submit it no record is added.
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'firstnameA', 'surnameA', 'example1#email.com', CURRENT_TIMESTAMP);
INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'firstnameB', 'surnameB', 'example#email.com', CURRENT_TIMESTAMP);
" ;
However this code works but i am only adding one record
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'tom', 'walker', 'tom#walker.com', CURRENT_TIMESTAMP);
"
You just need to duplicate the values portion, like this:
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`)
VALUES (NULL, 'tom', 'walker', 'tom#walker.com', CURRENT_TIMESTAMP),
(NULL, 'bob', 'jones', 'bob#jones', CURRENT_TIMESTAMP)";
Use mysqli_multi_query check out:
http://php.net/manual/en/mysqli.multi-query.php
I am in a process of converting my Existing MySQL to mysqli
But I can't get this piece of code correct
mysql_query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysql_insert_id();
mysql_query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");
What I tried is given below but didn't work
$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysqli_insert_id();
$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");
The Problem is with the $new_id = mysqli_insert_id(); statement bcoz the first query is executing
For mysqli Object oriented style to get the last inserted id use this
$mysqli->insert_id ;
http://www.php.net/manual/en/mysqli.insert-id.php
So your queries will be as
$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = $mysqli->insert_id;
$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");