Influencing how mysql-cli-client displays results from select queries - mysql

Is it possible to alter the way mysql on CL displays a result from select. This questions is aimed at a link or the propper term for this feature, so I can google it myself.
But specifically - is it possible to have a select result displayed as:
A: a
B: b
C: c
instead of:
A | B | C
----------
a | b | c
?

select * from Table\G
you end the query with a \G instead of a semicolon.

Related

select always x rows from table and start over when at the end

I want to create a PROCEDURE in MySQL that always returns x rows from a table, even when the table has less than x entries.
Like so:
+----+-------+
| id | value | CALL myProcedure USING(4);
+----+-------+ returns → a b c a
| 1 | a |
| 2 | b |
| 3 | c |
+----+-------+
Internally I store the last returned row (in this case it would be 'a') and on the next call the procedure should continue from there:
1st: CALL myProcedure USING(4) → a b c a
2nd: CALL myProcedure USING(3) → b c a
3rd: CALL myProcedure USING(7) → b c a b c a b
4th: CALL myProcedure USING(2) → c a
I tried it with UNION - this is what the 3rd call with x=7 would look like:
(
SELECT `value`
FROM `table`
LIMIT 1,7
)
UNION
(
SELECT `value`
FROM `table`
LIMIT 4
)
"give me as much as you can (up to 7) rows and start after row 1.
Then start over and give me the rest (7 - number of previous rows = 4)."
The first select returns b c and the second select returns a b c. Both selects together return b c a.
Now I am facing these problems:
1)UNION does not return the same row twice (all I would get from above call would be b c a)
2) At best I can "loop over my table" twice because there is only one union and I have no way of dynamically adding more unions. So even if I could get duplicate rows, it would only result in b c a b c and the remaining a b I expect will be missing.
How can I "loop" over my table multiple times? Is there anything better than UNION I could use?
EDIT (after solving my problem)
I followed cf_en's proposition to loop through the result set outside the database if the returned number of rows is less than expected. All other cases (those that only need one iteration) are covered by the procedure (using a simple UNION).
You can use UNION ALL to stop the union from removing duplicates. However, I wonder if the database is the best place to do the looping around. Could you return the distinct rows from the database and implement the looping logic in the calling code instead?

select multiple columns from multiple tables except certain columns

I have three tables.
The first table is like:
+----+----+----+
| id | x | y |
+----+----+----+
The second and third tables are like:
+----+----+----+----+----+----+----+----+----+----+----+----+
| id | Z1 | Z2 | Z3 | .. | .. | .. | .. | .. | .. | .. | Zn |
+----+----+----+----+----+----+----+----+----+----+----+----+
n is quite large, about 800-900.
I know it is quite ugly tables and database. But it is a raw data set and a learning set of a certain experiment. Please, just ignore it.
And a skeleton of a query is like:
'SELECT a.*, b.*, c.* \
FROM `test_xy` a, `test_1` b, `test_2` c \
WHERE a.id = b.id AND b.id = c.id'
What I concern is, the result with the query includes id field three times. I want id field to appear just one time at the front of the result.
I can do it by slicing the result table (by Python, MATLAB, etc.)
But, is there a better way to do this with a large number of columns? I mean, can id field of the second and third tables be excluded at the query stage?
The answer is the USING syntax: MySQL specific by the way. http://dev.mysql.com/doc/refman/5.5/en/join.html. Learn to use JOINs before you do anything else; putting the jon condition into the where clause is just plan wrong.
SELECT a.*, b.*, c.*
FROM `test_xy` a JOIN `test_1` b USING(`id)
JOIN `test_2` c USING(`id)

Multiple join table in mysql

mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>
This is the mysql join query tutorial on http://www.tutorialspoint.com/mysql/mysql-using-joins.htm. But I need to join many tables. In that case.. How WHERE statement would look like? Please help...
That's a very old-fashioned way of writing joins.
New and funky way is to write it like this:
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
FROM tutorials_tbl a
INNER JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author
To add more tables you just add more JOIN clauses. And since your tutorial seems a little outdated, here's the best explanation to joins I've ever seen, simple and beautiful and short: A Visual Explanation of SQL Joins
In FROM clause, you determine which tables to join via JOIN keyword, and describe common columns to pair using ON keyword.
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
FROM tutorials_tbl a
JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author
Or you may use USING since shared column names are equal:
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
FROM tutorials_tbl a
JOIN tcount_tbl b USING(tutorial_author)

SQL - Select multiple users from a table asoiciated with multiple options AKA Relations division

The problem is that I have a table where users have certain options "active" this is a representation of the table:
USER | OPTION
-------------
1 | A
1 | B
1 | C
2 | A
2 | D
2 | E
3 | A
3 | D
3 | M
let's assume we would like to select only those users that have both option A AND D (users 2 and 3 in this case).
I have thought something like nested SELECT, but I would like something that I could build with a loop, in fact OPTION will have about 40 distinct values and this query should be built with a form on the page.
any ideas?
unluckily the database "is done like this" and can not be changed (minor changes could be done, like add another field, but I can't change the logic behind)
select only those users that have both option A AND D
It is called relation division, here is one way to do so:
SELECT `user`
FROM tablename
WHERE `option` in ('A', 'D')
GROUP BY `user`
HAVING COUNT(DISTINCT `option`) = 2;
See it in action here:
SQL Fiddle Demo

Mysql returning multiple comma separated records

I'm trying to return a list of discussions and their attached queues (ids and names).
So far I have the following:
SELECT a.id as discussion_id,c.queue_id,e.queue_name
FROM support_discussions AS a
JOIN (
SELECT b.queue_id,b.discussion_id
FROM support_queues_discussions AS b
) AS c ON a.id=c.discussion_id
JOIN (
SELECT d.id,d.name AS queue_name
FROM support_queues AS d
) AS e ON c.queue_id=e.id
This returns the following (as expected):
discussion_id | queue_id | queue_name
1 | 1 | Queue name A
1 | 2 | Queue name B
What I'd really like to do is to get it to return each discussion as one line, along with separate columns for the queue id and the queue name:
discussion_id | queue_id | queue_name
1 | 1,2 | Queue name A,Queue name B
Any thoughts on how this can be done in an efficient manner?
there is GROUP_CONCAT function in mysql which does exactly what you want
did you think about what will happen if queue name contains comma character? maybe you should rethink your solution because what you described sounds way to dodgy
You can use GROUP_CONCAT.