Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output single column from db query without formatting #145

Closed
1 task done
petenelson opened this issue Jul 9, 2019 · 4 comments · Fixed by #249
Closed
1 task done

Output single column from db query without formatting #145

petenelson opened this issue Jul 9, 2019 · 4 comments · Fixed by #249

Comments

@petenelson
Copy link
Contributor

Feature Request

Describe your use case and the problem you are facing

The db query command currently formats the results in a table. While there is a --skip-column-names option (which doesn't show in the options when running help, only in the examples), there doesn't seem to be a way to disable the table formatting. While it may be possible to pass the results into a sed command to strip out the formatting, having it built-in without the extra steps may be handy.

Describe the solution you'd like

My use case was a query to get specific post IDs that I needed to pass to another command, ideally something like this:
wp db query --skip-column-names --format=csv "select ID from wp_4_posts where post_title like '%boston%' and post_type = 'hotel'"

This would give me just the post IDs in a list, which I could pipe to the paste command to turn them into a comma-delimited list, which I could pass to another command, ideally something like this, with a --column or --format flag.

wp elasticpress index --post-ids=$(wp db query --skip-column-names --column=ID "select ID from wp_4_posts where post_title like '%boston%' and post_type = 'hotel'" | paste -s -d, -)

@schlessera
Copy link
Member

Yes, that would prove useful indeed. Are you interested in producing a PR for this?

@petenelson
Copy link
Contributor Author

Yes, I'm interested. Not sure when I'll get to it, but I'd be glad to tackle this.

@SeanDS
Copy link

SeanDS commented Dec 23, 2019

I was surprised just now that wp db query doesn't support the --format flag, since most other commands do. This would be a good addition.

@dz0ny
Copy link

dz0ny commented Mar 3, 2020

There is mysql-shell https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-install-linux-quick.html which is almost drop-in replacement for mysql, and has output-format parameter. But this complicates soo many things.

I ended up using --skip-column-names --batch which returns each result delimited by a newline.

Full example:

while IFS= read -r domain; do
    echo "found $domain"
done < <(wp db query --skip-column-names --batch "select distinct(SUBSTRING_INDEX(SUBSTRING_INDEX(guid, '://', -1), '/', 1)) from wp_posts;")
found domain1.com
found domain2.com

Maybe --raw would be nice alias for --skip-column-names --batch ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants