Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 make a sql script out of this php

Author  Topic 

monkeys123
Starting Member

7 Posts

Posted - 2014-07-08 : 16:53:16
edited.
can anyone help me make a sql script out of this php... i dont know sql and am failing to learn.
<?php
$db = new PDO('mysql:host=localhost;dbname=wordpress;charset=utf8', 'root', '');
$query = "INSERT INTO $wpdb->terms (term_id, name, slug) VALUES (%d, %s, %s)";
$wpdb->query($wpdb->prepare($query, $term_id, $name, $slug));
foreach($db->query('SELECT * FROM wp_posts') as $row) {
global $wpdb;
$term_id = $row['ID'];
$name = $row['category'];
$slug = $row['category'];
$term_taxonomy_id = /*INCREASE THE BY 1(one) THE MAX TAZ_ID ALREDY INSERTED*/;
$taxonomy = 'category';
$query = "INSERT INTO $wpdb->term_taxonomy (term_taxonomy_id, term_id, taxonomy) VALUES (%d, %d, %s)";
$wpdb->query($wpdb->prepare($query, $term_taxonomy_id, $term_id, $taxonomy));
}
?>

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-07-08 : 16:57:54
[code]USE wordpress;

INSERT tmp
(
post_id,
slug,
tax,
term_id
)
SELECT ID,
category,
'category',
ISNULL((SELECT MAX(term_id) + 1 FROM dbo.tmp), 1)
FROM dbo.wp_posts;[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-07-08 : 16:58:41
change the inner select:
...
SELECT wp.ID, wp.category, 'category',(SELECT MAX(t.term_id) FROM tmp t)+1 FROM wp_posts wp
I don't know the logic that you are trying to implement, but be aware that if there is more than one row in wp_posts, all the new rows inserted into tmp will have the same term_id. And, if there are no rows already existing in tmp or if the term_id for for any existing rows are null, then the new rows will be inserted with a null.
Go to Top of Page

monkeys123
Starting Member

7 Posts

Posted - 2014-07-08 : 17:05:56
quote:
Originally posted by James K

change the inner select:
...
SELECT wp.ID, wp.category, 'category',(SELECT MAX(t.term_id) FROM tmp t)+1 FROM wp_posts wp
I don't know the logic that you are trying to implement, but be aware that if there is more than one row in wp_posts, all the new rows inserted into tmp will have the same term_id. And, if there are no rows already existing in tmp or if the term_id for for any existing rows are null, then the new rows will be inserted with a null.

i am confused too .-. term_id need to increase and not repeat (it will be a key in another table but inst the key of this table)

i maybe back -sissy exterminator
Go to Top of Page

monkeys123
Starting Member

7 Posts

Posted - 2014-07-08 : 17:17:38
its executing , i think i got it
USE wordpress;

INSERT tmp
(
post_id,
slug,
tax,
term_id
)
SELECT ID,
category,
'category',
(SELECT MAX(t.term_id) + 1 FROM tmp t)
FROM wp_posts;

all equal to 0 as expected the "ISNULL((SELECT MAX(term_id) + 1 FROM dbo.tmp), 1)" is givvin me a error as well
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-07-08 : 17:54:20
[code]DECLARE @t INT;

SELECT @t = ISNULL(MAX(term_id), 1)
FROM dbo.tmp;

INSERT tmp
(
post_id,
slug,
tax,
term_id
)
SELECT ID,
category,
'category',
ROW_NUMBER() OVER (ORDER BY post_ID) + @t
FROM dbo.wp_posts;[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

monkeys123
Starting Member

7 Posts

Posted - 2014-07-08 : 17:59:25
quote:
Originally posted by SwePeso

DECLARE	@t INT;

SELECT @t = ISNULL(MAX(term_id), 1)
FROM dbo.tmp;

INSERT tmp
(
post_id,
slug,
tax,
term_id
)
SELECT ID,
category,
'category',
ROW_NUMBER() OVER (ORDER BY post_ID) + @t
FROM dbo.wp_posts;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

sorry for taking your time but i think there is a error in my normalization, i need to learn some stuff before i can do this right
Go to Top of Page
   

- Advertisement -