SYNTAX CREATE VIEW SQL SERVER - sql-server-2008

i make a view in sql server and throw an error:
USE BaseDeDatos;
CREATE VIEW TEMAS_USUARIO
AS
SELECT TOP 5 t.id_userTopic, t.nameTopic, u.id_user, u.name
FROM Topic t, Users u
WHERE t.id_userTopic = u.id_group
ORDER BY t.id_topic DESC;
what is wrong whit the syntax?
error ---> CREATE VIEW TEMAS_USUARIO
can´t use LIMIT

USE BaseDeDatos
GO
CREATE VIEW TEMAS_USUARIO
AS
SELECT TOP 5 t.id_userTopic, t.nameTopic, u.id_user, u.name
FROM Topic t, Users u
WHERE t.id_userTopic = u.id_group
ORDER BY t.id_topic DESC;
The SQL SELECT TOP Clause:
SQL SERVER / MS ACCESS Syntax
SELECT TOP number|percent column_name(s)
FROM table_name;
MySQL Syntax
SELECT column_name(s)
FROM table_name
LIMIT number;
SQL SELECT TOP Clause explanation: Here

The syntax command of CREATE VIEW you have this:
The SELECT clauses in a view definition cannot include the following:
An ORDER BY clause, unless there is also a TOP clause in the select list of the SELECT statement
Important note Important
The ORDER BY clause is used only to determine the rows that are returned by the TOP or OFFSET clause in the view definition. The ORDER BY clause does not guarantee ordered results when the view is queried, unless ORDER BY is also specified in the query itself.

Related

Use result of WITH clause to filter WHERE ... IN columnName

I want to use the result of a WITH clause to filter a query like below:
WITH Max_Dates AS (
SELECT MAX(created_date) AS maxdate
FROM transactions
GROUP BY DATE (created_date)
)
SELECT *
FROM transactions
WHERE created_date IN Max_Dates -- can I somehow reference column maxdate as a list here?
The syntax is incorrect. I know I can use the content of the WITH clause as a subquery in the WHERE below to get the result I want, but I want to know specifically if the WITH result can be used.
My question is, is there a general syntax for using a column from a WITH clause to filter as a list in a WHERE ... IN {list}?
As per this blog it seems it should be possible to use WHERE created_at IN Max_Dates.maxdate, but I'm using MySQL 8.0.29 and it doesn't like that syntax - Error: (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 'Max_Dates.maxdate' at line 8")
WITH Max_Dates AS
(
SELECT MAX(created_date) AS maxdate
FROM transactions
GROUP BY DATE (created_date)
)
SELECT *
FROM transactions
WHERE created_date IN (select maxdate from Max_Dates)
The CTE (Common Table Expression) Max_Dates is a resultset that potentially has multiple columns, so you must specify the specific column from Max_Dates that should be used to build the list of values for the IN expression.

mysql query syntax error,LIMIT can't used before UNION any more after mysql8.0?

SQL:
select * from t_google_receipt limit 1 union select * from t_old_google_receipt limit 1;
the sql in my code excuted sucessful in my old mysql(Server version: 5.6.17-log MySQL Community Server (GPL)),but after updated to mysqlGroup(Server version: 8.0.11 MySQL Community Server - GPL), it excuted failed,
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 'union select * from t_old_google_receipt limit 1'
at line 1;
It actually changed in MySQL 5.7:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
Note
Previous versions of MySQL may permit such statements without
parentheses. In MySQL 5.7, the requirement for parentheses is
enforced.
Beware anyway that if you don't provide individual ORDER BY clauses you'll get arbitrary rows (not even random).
Use parentheses:
(select * from t_google_receipt limit 1)
union
(select * from t_old_google_receipt limit 1);
Notes:
You probably want union all and not union.
The use of limit without order by is suspicious.
This assumes that the columns in the two tables are the same, defined in the same order, and have compatible types.
For union and union all you have to consider below point
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
So in case of two tables union it is bad practice to use select all(select *) column from table rather it is preferable and less error to use column name explicitly like below
select col1,col2... coln from t1
union
select col1,col2.....coln from t2

SQL SELECT TOP statement is not working properly

I am trying to view the first 2 records of a table name Customers which have two columns name Name(varchar) and Salary(text) in MySQL server 6.0
The command which I am using is:
SELECT TOP 2 * FROM customers;
But it's not working.
I recommend you to write statement like this using LIMIT
SELECT * FROM customers [WHERE conditions] [ORDER BY expression [ASC|DESC]] LIMIT 2 ;
Instead of
SELECT TOP 2 * FROM customers;
It's very simple. Just try this
SELECT * FROM customers LIMIT 2;
You can check the manual also.
Use this in case of MySQL:
select * from customers limit 2;
To get any position value in the whole tale I suggest you to use the following query as TOP [position] doesn't work in all versions of MySQL.
select min(column_name) from (select column_name from table_name order by column_name desc limit position_you_sort) any_name_for_table;

Grouping two mySQL select statements under one alias and ordering that alias

I want to run two mysql SELECT statements, combine them, call the new combination by its own name, then order that new combination by a user-defined function. This is what I am trying currently:
SELECT * FROM (
SELECT * FROM dictionary WHERE def1 LIKE '$input%'
UNION ALL
SELECT * FROM dictionary WHERE def2 LIKE '$input%'
) AS newcol
ORDER BY levenshtein('$input', newcol)
LIMIT 10
But I get the following error:
Unable to run query:Unknown column 'newcol' in 'order clause'
The problem is clearly with defining the new group 'newcol'.
You're trying to order by a TABLE, not by a FIELD! Use a field from your tables ant it'll go smooth

Unknown column in order clause

#1054 - Unknown column 'default_ps_products.manufacturer_id' in 'order clause'
Why am I getting the above error with the statement below it works fine without the p in the statement and I am not using an order clause?
SELECT * FROM `default_ps_products` p WHERE p.`manufacturer_id` = 2
To solve this use SELECT p.* FROM instead of SELECT * FROM.
The reason is that phpMyAdmin is adding an ORDER BY to your query for the first column in the results grid. Because of the alias, the code that does this fails.
This issue reproduces on phpMyAdmin 4.0.6. I don't know the status on the latest 4.2.5
Since you posted a partial query this wasn't obvious from the start, but your full query makes it clear;
SELECT *
FROM default_ps_products
WHERE manufacturer_id=2
ORDER BY `default_ps_products`.`manufacturer_id` ASC
LIMIT 0, 30
When you add an alias to default_ps_products table in the select, you can't selectively use the alias only in the WHERE clause, you'll also need to change the ORDER BY to use the same alias. The full query should in other words be;
SELECT *
FROM default_ps_products p
WHERE p.manufacturer_id=2
ORDER BY p.`manufacturer_id` ASC
LIMIT 0, 30
Open your phpmyadmin. Click on your selected database. Now you have a list of all tables on right side. Click on structure of default_ps_products table. Now you see a structure of it. Now Click on SQL tab and execute query as 'SELECT * FROM default_ps_products ORDER BY '.
Once you execute this query, Now resolve your problem.
Your query is fine. there is not any error when i run this query. there is nothing wrong with query.
SELECT * FROM default_ps_products AS p WHERE p.manufacturer_id = 2
it working fine.:)
when use #Query(nativeQuery = true), should use underline format ,just like this "Sort.by(Sort.Direction.DESC, "update_time")))", else should use camel properties in entity ,like this "Sort.by(Sort.Direction.DESC, "updateTime")))"
phpmyadmin started showing this error suddenly on one table. only fix was to rename column id to id2 then back. even deleting the table and making new copy didn't help