Inserting content into MYSQL table columns with certain search criteria [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am pretty new to MYSQL tables, pretty limited experience.
I have a wordpress table (using PHPmyadmin) and basically, I want to add template content into all posts that have the below search criteria.
I honestly have no idea, I just need the bit where I can insert text (template text) all at once, not individually to the posts with the below criteria.
SELECT *
FROM `wp_posts`
WHERE `post_title` LIKE 'best hotel deals in%'
AND `post_status` LIKE 'draft'
Hope this makes sense!
OK, the post_content fields are blank at the moment and I want to fill it with basic text for example:
"the fox jumps over the fence"

you need to learn SQL, simple solution for you is:
UPDATE `wp_posts` set `colname`=CONCAT(`colname`, 'additional content')
WHERE `post_title` LIKE 'best hotel deals in%'
AND `post_status` LIKE 'draft'
if you want to update all posts - just remove where... condition

If you just want to add text to the beginning or end of the post content, you can do something like this:
update wp_posts
set post_content = concat(post_content, 'extra text you want to add')
where
post_title like 'best hotel deals in%' and
post_status = 'draft'
Be careful, though, particularly if you're not an experienced MySQL or WordPress user. Messing around directly in the database tables can really screw up your WordPress installation if you're not careful.

Related

MySQL Add tag to article if the keyword is in the article title [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This seems to be a bit out of my league and knowledge of MySQL (I'm not sure if it's even possible with MySQL). So if someone can help it would be much appreciated.
I would like to do the following:
Enter/define a keyword e.g. mercedes
Find all Joomla K2 articles that have that keyword in the title
Than to each of those articles assign my keyword mercedes as a tag.
Now... There are three tables with relevant columns listed:
k2_items
id, title
k2_tags
id, name, published (value 1 is assigned if tag is published)
k2_tags_xref
id, tagID, itemID
So, query should select all items from k2_items table that have keyword in their title, check if keyword is already defined as tag in k2_tags, if not than create a new tag. After that, new k2_tags_xref entry should be generated to connect keyword tag with K2 article item.
I still didn't have database course on my university so I'm kind of out of my league with this one, and it was supposed to be just a simple touchup for the site I'm developing.
Any help with this would be much appreciated, and I'm sure it'll help community later on.
Thanks!
The part in SQL is actually pretty easy. It is simplest if you have a unique index on k2_tags_xref(tagId, itemId). This has the database check for duplicates.
Then you need to do two things:
Find all items with the keyword in the title
Find the tagId for the keyword
This results in a query like this:
insert into k2_tags_xref(tagId, itemId)
select t.tagId, i.itemId
from k2_items i cross join
(select t.tagId from k2_tags where tag = 'mercedes') t
where i.title like '%mercedes%';
You will also need to put the tag into the tags table, if it is not already there. But the above query is the basis of the SQL code.

Get a data from a different table using id [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a table in my database called users and in this table there is a field called rank. This field holds the id of the users rank.
I also have a table called 'ranks' in my database, and this table has id, rank_id and rank_name, the id being the primary auto increment field, the rank_id hold the rank id and this is what is being used in my users table.
All is good. I need to display the users rank_name, not the rank_id when I output what rank they are. Im not a pro at mysql, i am still learning.
How do i get the users rank, match it up to the rank_id in the table ranks and then output the rank_name from the corresponding rank_id?
Please answer this using mysql_*. I know its life is ending soon but this is what im practicing with at the moment, will help me learn how to convert to mysqli_* or PDO.
You will need to use a query like this
SELECT u.name, r.rank_name
FROM users AS u
LEFT JOIN ranks AS r ON u.rank = r.rank_id
I would suggest you read about the different joins. Also remember you can join multiple tables in this fashion.

MySQL -- Keeping Track of User's Posts [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working on a project where users will be able to make posts that will be stored in a MySQL database. I need an efficient way to list the posts of one specific user and a way to list the posts of all users. I thought about using one table for all posts, but I am afraid that that would be slow. I could also have a different table for the posts of each user, but I would then not be able to search for posts by all users. Would it be efficient to combine the two methods above, or is there a better way?
For each post a user does, make note of his ID (a unique identifier) in the row, so that in the future you can select all the posts that a certain ID has done.
Example (pseudo sql structure):
tblPosts {
PostID int(11),
AuthorID int(11),
PostText text
}
tblAuthros {
AuthorID int(11),
AuthorName text
}
This is very efficient and fast
Create a table like so:
--posts--
id
post_body
user_id
date_posted
Where user_id is the unique ID of the user that created the post. Then select said posts like so:
SELECT id, post_body, user_id, date_posted FROM posts WHERE user_id = 8723

MySQL - Return all columns where bit is equal to 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working with a table that contains 50 columns (with bit values), for each 50 U.S. state. I'm trying to find an SQL statement that will return all columns containing a 1. I've looked around for ways of doing this with no luck.
Any help would be much appreciated
If this is a new development, and not for existing tables I would suggest the following. (Otherwise I'm stumped.) It sounds like you have implemented a many-to-many. I would suggest that you implement a table for States as suggested by #juergen d (albeit without the bit field); implement your current entity as-is but without the columns for states. And then implement a third table that has two columns, one for a state's ID and another for the ID of your entity.
Then instead of setting bit fields on your entity's table, you would create entries in this third table.
You can then perform joins on tables to obtain the states set on a certain entity.
For more info see http://en.wikipedia.org/wiki/Many-to-many_(data_model) and http://www.lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship
You should better change your table design. Better use something like this
States table
-------------------
id int
name varchar(100)
bitcol bit(1)
Then you could select that states like this
select name from States
where bitcol = 1

Draw The Highest Value Out Of A Mysql Database Column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a database "videos". Which has a table for each video i.e "ID" "Name" "Link" "Count".
i want to be able to take the highest value of any of the tables from the column "Count" and then use which ever has the highest values "Link" Column.
So the best way i can explain is i'm trying to make a "Most Viewed" part of my site so which ever has the highest hit count gets displayed via $row['link'].
Hope this made sense didn't much to me!
Thanks all in advance...
SELECT `id`,`name`,`link`,`count` from my_table order by `count` desc limit 5;
This will give you 5 most viewed links.
Tip : You should not use words like count as column names as they are MySQL keywords.
SELECT `link` FROM `someTable` ORDER BY `Count` DESC LIMIT 1
This will give you the link of the row with the highest count.

Categories