MySQL / JDBC how do you retrieve full terminal view of a query (Showing tables with +-) - mysql

How do you retrieve the terminal view of a mysql select query?
Desired Result: below ; how do I get that in my result set? as opposed to just the numbers for specific columns?
+---------+----------+
| Dog_ID | Owner_ID |
+---------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+---------+----------+

MySQL uses that exact output in the Console. Open the MySQL Command Line Client that comes with MySQL installation and type your select like
select * from your_table;
Then copy the result out of the console output.

Related

MS Access - Data Type Mismatch in Criteria Expression

Using the query grid , comparing a String field with a Replace function result of another String field (same table) results in a Data Type Mismatch error when trying to filter for ‘Not Like’ (or <>).
‘TypeName’ confirms that all records are of type “String”.
The problem is caused by “MyStrCalc: Replace([StrA],".","_")” which is compared with StrB. StrA contains Null for some records. These are filtered out (Criterium = “Is Not Null”). But even when creating a new query that uses the result of the first, the same error occurs. I have also tried Nz.
If I use Make Table to create a new table where StrA “Is Not Null” and run effectively the same query, there’s no issue.
The data in the table changes frequently, so having to create a separate table every time (tens of thousands of records) is a real nuisance.
Any suggestions how to make the query work would be greatly appreciated.
(By the way – the version used is MS Access 2019 under Windows 10, both with latest updates.)
Thank you for your much appreciated quick reply.
I tried a few things as detailed below with the fourth attempt providing the desired result.
Source table t1:
| UID | StrA | StrB |
| ---:| ----- | ----- |
| 1 | Str.1 | Str_1 |
| 2 | | Str_2 |
| 3 | Str.3 | Str_4 |
Desired Result = StrA<>StrB after replacing dots in StrA with underscores:
| UID | StrA | StrB
| ---:| ----- | -----
| 2 | | Str_2
| 3 | Str.3 | Str_4
q1_Bad:
SELECT t1.UID, t1.StrA, t1.StrB, Replace([StrA],".","_",1,-1,1) AS StrACalc
FROM t1
WHERE (((Replace([StrA],".","_",1,-1,1)) Not Like [StrB]));
Result: “Data type mismatch in criteria expression”.
q2_Runs_CannotFilter:
SELECT t1.UID, t1.StrA, t1.StrB, Replace([StrA],".","_",1,-1,1) AS StrACalc, [StrACalc] Not Like [StrB] AS StrACalc_NtEq_StrB
FROM t1
WHERE (((t1.StrA) Is Not Null));
Result: Runs, but filtering field ‘StrACalc_NtEq_StrB’ (SQL or after running query) results in “Data type mismatch in criteria expression”.
q3_OK_SQL_FilterFail:
SELECT t1.UID, t1.StrA, t1.StrB, Replace(Nz([StrA]),".","_",1,-1,1) AS StrACalc, Nz([StrACalc] Not Like [StrB]) AS StrACalc_NtEq_StrB
FROM t1;
Result: Runs, but filtering field ‘StrACalc_NtEq_StrB’ is only possible after running query. Adding “Nz([StrACalc] Not Like [StrB]) AS StrACalc_NtEq_StrB” results in “Enter Parameter Value | StrACalc”.
Note: If the result of the above is called in another query, the SQL filtering will work.
q4_OK
SELECT t1.UID, t1.StrA, t1.StrB
FROM t1
WHERE (t1.StrB) Not Like Replace(Nz([StrA]),".","_",1,-1,1);
Finally – Desired result:
| UID | StrA | StrB |
| ---:| ----- | ----- |
| 2 | | Str_2 |
| 3 | Str.3 | Str_4 |

Query works on PhpMyadmin but not on my server

Here are my tables:
'modules-settings' with columns: module_ticker | name | module_code |.
'modules-users' with columns: | role | module_id | module_order |.
This query #1 works:
SELECT `modules-settings`.module_code
FROM `modules-settings`
JOIN `modules-users`
ON `modules-users`.module_id=`modules-settings`.module_ticker WHERE role='N'
but not query #2:
SELECT `modules-settings`.module_code, `modules-users`.module_order
FROM `modules-settings`
JOIN `modules-users`
ON `modules-users`.module_id=`modules-settings`.module_ticker WHERE role='N'
The SQL error is "Could not execute the query". The strange thing is that query #2 works when I test it on PhpMyAdmin.
What am I missing here? Thank you for your help.
I saw this once where the mysql client on the server was a different version from the mysql server. Please check that your client and server are the same version. phpmyadmin bypasses the client and puts the query directly to the server, so phpmyadmin would work, even if the mysql client is a different version.
Make sure default database is the db that you are going to access.
try following query with after alter the tables names
SELECT ms.module_code
FROM modules_settings ms
JOIN modules_users mu ON mu.module_id=ms.module_ticker
please try using this query :
SELECT ms.module_code, mu.module_order
FROM modules-settings ms
JOIN modules-users mu
ON mu.module_id=ms.module_ticker WHERE role='N'
please try using this :
my database table :
I have created two tables:
SELECT * FROM `modules-settings` ;
module_ticker | name | module_code |
+---------------+----------+--------------+
| 1 | test | testcode |
| 2 | demotest | demotestcode |
SELECT * FROM `modules-users` LIMIT 0 , 30;
+------+-----------+--------------+
| role | module_id | module_order |
+------+-----------+--------------+
| N | 1 | 123 |
| 0 | 2 | 456 |
+------+-----------+--------------+
Now execute query :
SELECT `modules-settings`.module_code, `modules-users`.module_order FROM `modules-settings` JOIN `modules-users` ON `modules-users`.module_id=`modules-settings`.module_ticker WHERE role='N';
+-------------+--------------+
| module_code | module_order |
+-------------+--------------+
| testcode | 123 |
+-------------+--------------+
#Aidan pointed me to the solution. He knew the queries were fine so tried to find another reason by thinking out of the box. the pb was indeed that I was running these queries in 2 databases very similar but still different enough for my pb to occur.

why does this query return an empty set in mysql

I'm new to MySQL and I'm using jdbc to connect with MySQL server. My current subjects table in the database with its entries are.
+--------+-----------+--------+---------+-------+----------+
| sub_no | name | lec_no | credits | price | semester |
+--------+-----------+--------+---------+-------+----------+
| cs001 | network 1 | lec001 | 3 | 3000 | 7 |
+--------+-----------+--------+---------+-------+----------+
but when I run SELECT credits,price,semester from subjects WHERE sub_no ="cs001" why does MySQL return an empty set instead of 3, 3000, 7
I think it's because mysql is searching for "cs001" and not cs001. Try removing the quotes and see what happens.

MySQL query to SELECT rows with LIKE and create new column containing the matched string

I need some help with a MySQL query I am struggling for some time now.
So, I am trying to create a MySQL query to SELECT rows from a table that match a specific string like app.
My table is like this:
+-----+--------------+
| id | name |
+-----+--------------+
| 1 | Green Apple |
| 2 | Big Orange |
| 3 | application |
+-----+--------------+
I can find all rows that contain app string with SELECT and LIKE.
However, I also want to create new column that contains the string from name column which matches app and keep the database case sensitive format, i.e. with app as a match phrase the new column will contain App and app entires according to the string format in name.
My query so far goes like this:
SELECT *, 'what_to_put?' as new_column FROM table WHERE name LIKE '%".$app."%'
The desired output is the following:
+-----+--------------+-------------+
| id | name | new_column |
+-----+--------------+-------------+
| 1 | Green Apple | App |
| 2 | application | app |
+-----+--------------+-------------+
Any idea how to achieve this?
Without a separate regex library, you'll need to use the built-in string functions to find the location of the match, and then extract the matching sub-string:
SELECT
id,
name,
substring(name, locate('app', name), length('app')) as new_column
FROM yourTable
WHERE name LIKE '%app%'
Which gives the results:
+----+-------------+------------+
| id | name | new_column |
+----+-------------+------------+
| 1 | Green Apple | App |
| 3 | application | app |
+----+-------------+------------+
Sql Fiddle Here

Display only one row for values that appear multiple times

I have multiple rows with the same name in this table, and I want to show only one of row of each. For example, with the following data:
| name | number |
+------+--------+
| exe | 1 |
| exe | 10 |
| exe | 2 |
| bat | 1 |
| exe | 3 |
| bat | 4 |
I would like to see the following results:
| name | number |
+------+--------+
| exe | 16 |
| bat | 5 |
How can I achieve this result?
Duplicate response: My question only have 1 table, the JOIN ..ON command creates confusion in understanding, i think this simple question can help many guys!
Try something like this:
SELECT t.`name`, SUM(t.`number`) AS `number`
FROM mytable t
GROUP BY t.`name`
ORDER BY `number` DESC
let the database return the result you want, rather than mucking with returning a bloatload of rows, and collapsing them on the client side. There's plenty of work for the client to do without doing what the database can do way more efficiently.
You can use an aggregation function for this:
SELECT name, SUM(number) AS total
FROM myTable
GROUP BY name;
Here is a reference on aggregate functions, and here is an SQL Fiddle example using your sample data.