mysql is weirdly formatting my output even though the table isnt overflowing with data in any way (only 30-4 rows, and 4 columns).
Is there something I can do to adjust this?
mysql> select id, city, state, zip from location;
+----+----------------+-------+-------+
| id | city | state | zip |
+----+----------------+-------+-------+
| 97227 |and | OR
| 95814 |mento | CA
| 94607 |nd | CA
| 90245 |gundo | CA
| 90015 |ngeles | CA
| 85004 |ix | AZ
| 84101 |Lake City | UT
| 80204 |r | CO
| 78219 |ntonio | TX
| 77002 |on | TX
| 75219 |s | TX
| 73102 |oma City | OK
| 70113 |rleans | LA
| 60612 |go | IL
| 55403 |apolis | MN
| 53203 |ukee | WI
| 48326 |n Hills | MI
| 46204 |napolis | IN
| 44115 |land | OH
| 38103 |is | TN
| 33132 | | FL
| 32801 |do | FL
| 30303 |ta | GA
| 28202 |otte | NC
| 20004 |ngton | DC
| 19148 |delphia | PA
| 11217 |lyn | NY
| 10121 |ork | NY
| 29 | Boston | MA | 2114 |
+----+----------------+-------+-------+
29 rows in set (0.00 sec)
Somehow you got carriage returns at the end of most of the state values. You can remove them with:
UPDATE location SET state = TRIM(TRAILING '\r' FROM state);
And you should investigate the code you use to add rows to this table, to see why it's leaving those characters in the data. You're probably using a file that was created on Windows and loading it into a program that runs on Unix. You can use the dos2unix command on Linux to fix all the newlines in a file. Or you can fix the program so it removes extraneous carriage return characters.
Related
I have been trying to find duplicate and got confused by 3 lines of codes. Can you please help me understand it?
Please find DESC of my table :
| Field | Type | NULL | Key | Extra |
| id | int(11) | NO | PRI | auto_increment |
| name | varchar(50) | YES | | |
| membership | enum('Silver','Gold','Diamond') | YES | | |
| interest |set('Movie','Music','Concert') | YES | | |
This is the output for all the present data in table.
Select * from clients
+----+--------+------------+---------------+
| id | name | membership | interest |
+----+--------+------------+---------------+
| 1 | Sourav | Silver | Movie,Concert |
| 2 | Yash | Diamond | Music |
| 3 | Yash | Diamond | Music |
| 4 | Yash | Diamond | Music |
| 5 | Yash | Diamond | Music |
| 6 | Yash | Diamond | Music |
| 7 | Yash | Diamond | Music |
| 8 | Yash | Diamond | Music |
| 9 | Yash | Diamond | Music |
| 10 | Yash | Diamond | Music |
| 11 | Yash | Diamond | Music |
| 12 | Yash | Diamond | Music |
| 13 | Yash | Diamond | Music |
| 14 | Yash | Diamond | Music |
| 15 | Sneha | Silver | Concert |
+----+--------+------------+---------------+
15 rows in set (0.001 sec)
Confusion arose in below three queries for why do they return 3 completely different outputs?
1.
SELECT id, name, membership, interest, count(*)
FROM clients
GROUP BY name, membership, interest
HAVING count(*) > 1;
//This one being the correct line of code
+----+------+------------+----------+----------+
| id | name | membership | interest | count(*) |
+----+------+------------+----------+----------+
| 2 | Yash | Diamond | Music | 13 |
+----+------+------------+----------+----------+
SELECT name, membership, interest, count()
FROM clients
HAVING count() > 1;
//Returns first entry with count as 15
+--------+------------+---------------+----------+
| name | membership | interest | count(*) |
+--------+------------+---------------+----------+
| Sourav | Silver | Movie,Concert | 15 |
+--------+------------+---------------+----------+
SELECT id, name, membership, interest, count(*)
FROM clients
GROUP BY name, membership, interest;
//Returns Unique Entries only while eliminating the duplicate entries. However, Sneha (last entry) was on top for some reason
+----+--------+------------+---------------+----------+
| id | name | membership | interest | count(*) |
+----+--------+------------+---------------+----------+
| 15 | Sneha | Silver | Concert | 1 |
| 1 | Sourav | Silver | Movie,Concert | 1 |
| 2 | Yash | Diamond | Music | 13 |
+----+--------+------------+---------------+----------+
What necessary changes happened when I used statement because given same syntax without statement clause, it was working completely different.
In addition to that the last statement query was intriguing me as even though I did not apply any clause for Unique data, it delivered the same to me.
Finally, why did the last entry appeared at top in the 3. query?
Correct query finding dupes in your case should be (one possibility as might be other solution as well),
select min(id) as id,
name,
membership,
interest,
count(*) as cnt
from clients
group by name,membership,interest
having count(*) > 1;
https://dbfiddle.uk/HsV-S897
As per your tries.
The first query should fail with sql_mode only_full_group_by enabled which should be enabled .
In the second query HAVING without GROUP BY acts like WHERE, still the second query will fail with sql_mode only_full_group_by enabled.
The third query is correctly formed but you need order by count(*) desc to get Yash first.
I have question about SELECT FROM WHERE statement, which returns me bad result.
Here is my table called friends:
+----------+-----------+------------+--------+--------+-------+
| lastname | firstname | callprefix | phone | region | zip |
+----------+-----------+------------+--------+--------+-------+
| Lužný | Bob | 602 | 111222 | OL | 79821 |
| Matyáš | Bob | 773 | 123456 | BR | NULL |
| Strouhal | Fido | 300 | 343434 | ZL | 76701 |
| Přikryl | Tom | 581 | 010101 | PL | 72000 |
| Černý | Franta | 777 | 000999 | OL | 79801 |
| Zavadil | Olda | 911 | 111311 | OL | 79604 |
| Berka | Standa | 604 | 111234 | ZL | 72801 |
| Vlcik | BbB | 736 | 555444 | KV | 35210 |
+----------+-----------+------------+--------+--------+-------+
And here is my query.
SELECT * FROM friends WHERE region <= 'z';
I would expect that the rows with region ZL should be present, but they are not. Can you please tell me why?
Result is:
+----------+-----------+------------+--------+--------+-------+
| lastname | firstname | callprefix | phone | region | zip |
+----------+-----------+------------+--------+--------+-------+
| Lužný | Bob | 602 | 111222 | OL | 79821 |
| Matyáš | Bob | 773 | 123456 | BR | NULL |
| Přikryl | Tom | 581 | 010101 | PL | 72000 |
| Černý | Franta | 777 | 000999 | OL | 79801 |
| Zavadil | Olda | 911 | 111311 | OL | 79604 |
| Vlcik | BbB | 736 | 555444 | KV | 35210 |
+----------+-----------+------------+--------+--------+-------+
When I try this query:
SELECT * FROM friends WHERE region >= 'z';
the result contains both rows with region = 'ZL'
????
Thank you!
Because "ZL" is greater than "Z." Z is just one character so will only return values less that Z or with the value of Z. What are you trying to achieve with this query?
Can you please tell me why?
If you add a record where region is Z, and sorted those rows alphabetically by region, would you expect ZL to come before or after Z? Obviously it would come after, so it does not meet your criteria.
If you want to only consider the first character, then add that to your criteria:
SELECT * FROM friends WHERE LEFT(region,1) <= 'Z';
I would also make Z explicitly a capital letter in case your database settings make it a case-sensitive search.
Have you tried
SELECT * FROM friends WHERE region <= 'zl';?
From the computer's perspective, 'z' < 'zl'
I am using the wrong syntax. It's confusing as a beginner to get mixed up with using the comma, quotation and the tic mark.
-list the form of government for the countries with the top 5 average GNP.
Here is the statement that I am using and the results.
SELECT 'avg-gnp','form-government'
FROM country
ORDER BY 'avg-gnp'
DESC LIMIT 5;
+---------+-----------------+
| avg-gnp | form-government |
+---------+-----------------+
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
| avg-gnp | form-government |
+---------+-----------------+
5 rows in set (0.01 sec)
I've also tried this: Thanks in advance!
mysql> SELECT * FROM country ORDER BY 'avg-GNP' DESC LIMIT 5;
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
| code | fullname | continent | region | area | year-independence | population | avg-lifespan | avg-GNP | form-government |
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
| ABW | Aruba | North America | Caribbean | 193 | 0 | 103000 | 78.40 | 828.00 | Nonmetropolitan Territory of The Netherlands |
| AFG | Afghanistan | Asia | Southern and Central Asia | 652090 | 1919 | 22720000 | 45.90 | 5976.00 | Islamic Emirate |
| AGO | Angola | Africa | Central Africa | 124670 | 1975 | 12878000 | 38.30 | 6648.00 | Republic |
| AIA | Anguilla | North America | Caribbean | 96 | 0 | 8000 | 76.10 | 63.20 | Dependent Territory of the UK |
| ALB | Albania | Europe | Southern Europe | 28748 | 1912 | 3401200 | 71.60 | 3205.00 | Republic |
+------+-------------+---------------+---------------------------+--------+-------------------+------------+--------------+---------+----------------------------------------------+
5 rows in set (0.00 sec)
This is a comment that cannot be put in a comment since comments don't allow back ticks. I wanted to make sure you got it right.
Correct Form (this is how it should look using back ticks):
SELECT `avg-gnp`,`form-government`
FROM country
ORDER BY `avg-gnp`
DESC LIMIT 5;
Wrong Form (this is the way you had it using single quotes):
SELECT 'avg-gnp','form-government'
FROM country
ORDER BY 'avg-gnp'
DESC LIMIT 5;
Do you see the difference?
And yes, MySQL is weird about this. The only database I know that uses back ticks (well... and MariaDB, of course).
I am having 2 tables in Microsoft access.
1st table name - details
2nd table name - code
in 1st table I have more than 15 columns, I need to insert one column after 10th column and rename the heading as "TYPE".
in 2nd table I have 5 column.
Common column in each table is Analysis code.
Table 1
+-------+-------+---------------+------------+---------+
| Test1 | test2 | Analysis code | test4 | test5 |
+-------+-------+---------------+------------+---------+
| ab | dfd | TON | fddafd | 212132 |
| ced | fd | SIN | 2133 | dfd2fd1 |
| ef | fdfd | TON | df2df1d31f | dfd3sa3 |
| gh | dfd | SIN | dfd63 | c22 |
+-------+-------+---------------+------------+---------+
Table 2
+----------+---------------+----------+------------------------+
| sample 1 | Analysis code | sample 3 | Type |
+----------+---------------+----------+------------------------+
| 558825 | TON | a | Terminated on network |
| 258c | SIN | b | International network |
| 5856c | TOF | c | Terminated off network |
| a21c5b | SMS | d | text message |
+----------+---------------+----------+------------------------+
OUT PUT Table
+-------+-------+---------------+-----------------------+------------+---------+
| Test1 | test2 | Analysis code | Type | test4 | test5 |
+-------+-------+---------------+-----------------------+------------+---------+
| ab | dfd | TON | Terminated on network | fddafd | 212132 |
| ced | fd | SIN | International network | 2133 | dfd2fd1 |
| ef | fdfd | TON | Terminated on network | df2df1d31f | dfd3sa3 |
| gh | dfd | SIN | International network | dfd63 | c22 |
+-------+-------+---------------+-----------------------+------------+---------+
You need to join the table1 and Table2 on the corresponding column. In your case "Analysis code"
a sample would be:
SELECT Test1, test2, [table1.Analysis code], table2.type .... rest
FROM table1 left join table2 on table1.[Analysis code] = tble2.[analysis code]
above SQL code will produce [somewhat]your expected output table. Having said that, please allow me to suggest, that you need to read more about SQL language and [relational databases]. will help you more!
I have a table which requires me to pair certain rows together using a unique value that both the rows share.
For instance in the below table;
+--------+----------+-----------+-----------+----------------+-------------+
| id | type | member | code | description | matching |
+--------+----------+-----------+-----------+----------------+-------------+
| 1000 |transfer | 552123 | SC120314 | From Gold | |
| 1001 |transfer | 552123 | SC120314 | To Platinum | |
| 1002 |transfer | 833612 | SC120314 | From silver | |
| 1003 |transfer | 833612 | SC120314 | To basic | |
| 1004 |transfer | 457114 | SC150314 | From Platinum | |
| 1005 |transfer | 457114 | SC150314 | To silver | |
| 1006 |transfer | 933276 | SC180314 | From Gold | |
| 1007 |transfer | 933276 | SC180314 | From To basic | |
+--------+----------+-----------+-----------+----------------+-------------+
basically What i need the query / routine to do is find the rows where the value in the 'member' column for each row match. Then see if the values in the 'code' column for the same found rows also match.
If both columns for both rows match, then assign a value to the 'matching' column for both rows. This value should be the same for both rows and unique to only them.
The unique code can be absolutely anything, so long as it's exclusive to matching rows. Is there any query / routine capable of carrying this out?
I'm not sure I understand the question correctly, but if you like to pick out and update rows where the code and member columns matches and set matching to some unique value for each of the related rows, I believe this would work:
UPDATE <table> A
INNER JOIN (SELECT * FROM <table>) B ON
B.member = A.member && B.code = A.code && A.id <> B.id
SET A.matching = (A.id + B.id);
The matching value will be set to the sum of the id columns for both rows. Notice that updating the matching field this way will not work if there are more than two rows that can match.
Running the above query against your example table would yield:
+------+----------+--------+----------+---------------+----------+
| id | type | member | code | description | matching |
+------+----------+--------+----------+---------------+----------+
| 1000 | transfer | 552123 | SC120314 | From Gold | 2001 |
| 1001 | transfer | 552123 | SC120314 | To Platinum | 2001 |
| 1002 | transfer | 833612 | SC120314 | From Silver | 2005 |
| 1003 | transfer | 833612 | SC120314 | To basic | 2005 |
| 1004 | transfer | 457114 | SC150314 | From Platinum | 2009 |
| 1005 | transfer | 457114 | SC150314 | To silver | 2009 |
| 1006 | transfer | 933276 | SC180314 | From Gold | 2013 |
| 1007 | transfer | 933276 | SC180314 | From To basic | 2013 |
+------+----------+--------+----------+---------------+----------+
I can give you a simple query what can do what you need.
tst is the name of the table.
SELECT *, COUNT( t2.id ) as matching FROM tst t LEFT JOIN tst t2 ON t2.member = t.member GROUP BY t.id