How to get the last 3 rows from MYSQL database - mysql

I wanted to get only the last 3 rows of my database here is the query that I tried
select top 3 * from poem where MemberID = 54;
But there was an error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '3 * from poem where MemberID = 54' at line 1
This is an example of one of the data stored in my table poem
MariaDB [bigproject]> select PoemID, Title, Description from poem where MemberID = 79;
+--------+------------------------+--------------------+
| PoemID | Title | Description |
+--------+------------------------+--------------------+
| 34 | Everything Has Changed | EHC |
| 52 | Kapono | Poetic |
| 53 | Love under the sky | How is your heart? |
| 54 | Imaginary | Imagine me |
+--------+------------------------+--------------------+
4 rows in set (0.000 sec)
I only wanted to get the last 3 rows of this.
Thank you!!

Related

How can I LEFT JOIN a table and COUNT the rows?

I want to list my projects and the amount of posts each project has:
`projects` `posts`
------------------ ------------------
| `id` | `name` | | `id` | `name` |
------------------ ------------------
| 1 | Cat | | 1 | Cat |
| 2 | Dog | | 2 | Cat |
| 3 | Bird | | 3 | Dog |
| 4 | Frog | ------------------
------------------
$projects = $db->query('SELECT *,
COUNT(posts.*) AS posts
FROM projects
LEFT JOIN posts ON projects.name=posts.name
')->fetchAll(PDO::FETCH_ASSOC);
foreach($projects as $row) {
echo "Project ".$id." has ".$posts ".posts.".<br>";
}
So as a result I expect:
Project 1 has 2 posts.
Project 2 has 1 posts.
Project 3 has 0 posts.
Project 4 has 0 posts.
But I get an error message:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '*) AS posts,
So I changed posts.* into posts.id
But then my result is:
Project 1 has 3 posts.
I am actually now confused how to achieve the result I need.
The error was raised because of count(po.*) you must count for one column only inside of a count() function.
So, we want to find out how many times will find the 'name' column in posts table for a corresponding id column in projects table so we should count posts.name from posts joined with projects.
We need left join because we want also find out if it does not have corespondent (count(po.name) =0))
The query below will do the job:
SELECT p.*,COUNT(po.name) AS posts
FROM projects p
left join posts po on p.name = po.name
group by p.id
You can check here: http://sqlfiddle.com/#!9/3e9d4b/4

MySql Query regarding use of TOP command

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '(2) from empdata where empid='E-713'' at line 1
mysql> delete TOP(2) from empdata where empid='E-713';
+-------+--------+--------+--------+------+---------+
| id | name | height | salary | age | city |
+-------+--------+--------+--------+------+---------+
| E-713 | Rajat | 5.11 | 25000 | 25 | jaipur |
| E-720 | Ritesh | 5.8 | 30000 | 27 | Delhi |
| E-711 | Javed | 5.7 | 23000 | 25 | kashmir |
| E-715 | Puneet | 5.1 | 20000 | 27 | Noida |
| E-713 | Rajat | 5.11 | 25000 | 25 | jaipur |
+-------+--------+--------+--------+------+---------+
How can I get nth highest salary using top command in MySql. although my syntax is correct as per my knowledge but on pressing enter the above error flashed.
This is a bit long for a comment.
MySQL does not support SELECT TOP. That is usually associated with SQL Server.
It does support LIMIT, so you could write:
delete ed
from empdata ed
where empid = 'E-713'
limit 2;
However, this is very dangerous, because it deletes two arbitrary rows. In almost all cases, you want an ORDER BY:
delete ed
from empdata ed
where empid = 'E-713'
order by ??
limit 2;
This is true whether you are using TOP or LIMIT.

MySQL Cannot Show Column Name "Group"

I have a table which:
| fullname | onepreference | group | batch |
+------------+---------------+-------+-------+
| First Name | 1 | 1 | 2015 |
| First Name | 2 | 1 | 2015 |
| First Name | 1 | 2 | 2007 |
| First Name | 4 | 2 | 2014 |
+------------+---------------+-------+-------+
I am trying to write the following query:
SELECT
fullname,
onepreference,
group
FROM mytable
WHERE batch = 2015;
But it shows the following error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'group from mytable where batch=2015' at line 1
I think group column is a keyword of mysql. But I want to get the data of group column. Is there any possibilities available to take the group value?
group is a reserved word in mysql. so you have to escape it whith backticks:
select fullname, onepreference, `group` from add_application_form where batch=2015;
group is a reserved word and you need to use backticks:
SELECT
fullname,
onepreference,
`group`
FROM add_application_form
WHERE batch=2015;
Check the list here and avoid those keyword as table and column names.

How do I get rhe right syntax for searching a string in a database table?

List the state descriptions that start with “NEW” and the count of the employers located in that state. Be sure to list all of those states that start with “NEW” even if the count is zero. Make sure your column headings match what is shown below.
+---------------+---------------------+
| Description | Number of Companies |
+---------------+---------------------+
| NEW HAMPSHIRE | 0 |
| NEW JERSEY | 1 |
| NEW MEXICO | 0 |
| NEW YORK | 13 |
+---------------+---------------------+
4 rows in set (0.00 sec)
For this question I used:
SELECT state.description, COUNT(*) "Number of Commpanies"
FROM employer
WHERE SUBSTR(state.description, 1, INSTR(state.description, 'NEW')-1) AS "Number of Companies";
ERROR:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'AS "N
umber of Companies"' at line 3
Why am I getting this and what is the right syntax. First of all, I'm not sure if I'm following the question correctly. The table as follows:
mysql> DESCRIBE state;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| statecode | char(2) | | PRI | | |
| description | varchar(30) | | | | |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql>
mysql>SELECT * FROM employer
| companyname | division | address | city | statecode | zipcode |
| Acme Information Source | Customer Support | 132 Commerical Way | Cleveland | OH | 44234 |
| Ajax Software, Inc. | RandD | 2421 West Industrial Way | Berkeley | CA | 94710 |
| Ajax Software, Inc. | Production | 2421 West Industrial Way | Berkeley | CA | 94710 |
The correct syntax for a column alias is:
SELECT state.description, COUNT(*) AS "Number of Companies"
FROM employer
WHERE .. -- your logic here
You can't add a column alias after the WHERE, that doesn't make sense.
However, you have a way to go to actually answering the question. You refer to the state table, but you're not joining to it yet, for example.
A where clause has to resemble
where something = something
Yours resembles
where something as alias name
However, the simple answer is:
where state.description like 'NEW%'
Try the following:
SELECT s.description, count(e.*) as "Number of Companies"
FROM employer e left join state s on e.statecode = s.statecode
WHERE s.description like 'NEW%'
GROUP by 1
Use a JOIN, and you were missing a GROUP BY
SELECT
s.description,
COUNT(*) AS "Number of Companies"
FROM state a
JOIN employer e ON s.statecode = e.statecode
WHERE s.description LIKE 'NEW%'
GROUP BY 1

Updating multiple columns with data from subquery in MySQL

I am trying to update multiple columns in a row, with data from multiple columns in a subquery.
The following approaches did not work for me, and I can't find different ones that suit my needs:
UPDATE
beers,
(SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AVG(taste) AS taste, AVG(aroma) AS aroma, AVG(overall) AS overall, beer_id FROM reviews) AS review_total
SET
beers.appearance = review_total.appearance,
beers.palate = review_total.palate,
beers.taste = review_total.taste,
beers.aroma = review_total.aroma,
beers.overall = review_total.overall
WHERE
review_total.beer_id = beers.id
AND
beers.id = 43
I don't get an error for this one, but 5 warnings and the row is not updated:
Query OK, 0 rows affected, 5 warnings (0.01 sec)
Show warnings gives me:
+-------+------+----------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------+
| Note | 1265 | Data truncated for column 'appearance' at row 9991 |
| Note | 1265 | Data truncated for column 'palate' at row 9991 |
| Note | 1265 | Data truncated for column 'taste' at row 9991 |
| Note | 1265 | Data truncated for column 'aroma' at row 9991 |
| Note | 1265 | Data truncated for column 'overall' at row 9991 |
+-------+------+----------------------------------------------------+
I know this issue has to do with the data type, but the data type is float, i beleive thats what AVG's result is too:
mysql> describe beers;
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(90) | YES | | NULL | |
| aroma | float | YES | | NULL | |
| appearance | float | YES | | NULL | |
| palate | float | YES | | NULL | |
| taste | float | YES | | NULL | |
| overall | float | YES | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
The next query is slightly different:
UPDATE
beers
SET
beers.appearance = review_total.appearance,
beers.palate = review_total.palate,
beers.taste = review_total.taste,
beers.aroma = review_total.aroma,
beers.overall = review_total.overall
FROM
INNER JOIN (SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AVG(taste) AS taste, AVG(aroma) AS aroma, AVG(overall) AS overall, beer_id FROM reviews) review_total ON review_total.beer_id = beers.id
WHERE
beers.id = 43
The error i got for this one is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM INNER JOIN (SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AV' at line 9
I really can't find a way to get this working and I hope someone sees what I'm doing wrong. Thank you very much in advance!
UPDATE beers b
JOIN
( SELECT beer_id
, AVG(appearance) appearance
, AVG(palate) palate
, AVG(taste) taste
, AVG(aroma) aroma
, AVG(overall) overall
, beer_id
FROM reviews
GROUP
BY beer_id
) review_total
ON review_total.beer_id = b.id
SET b.appearance = review_total.appearance
, b.palate = review_total.palate
, b.taste = review_total.taste
, b.aroma = review_total.aroma
, b.overall = review_total.overall
WHERE b.id = 43;
or something like that