Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
4
votes1
answer5739
viewsIs it possible to generate a table script from an SQL command?
I would like to generate a table creation script, from an SQL command so I could build a program that was able to clone tables. I am using SQL Server 2008 R2 and need to implement a table cloning…
-
4
votes1
answer802
viewsHow to model product categories?
I am about to start the development of an e-commerce, but I am focused on the issue of modeling, especially regarding categories and subcategories of products. For example. A category can have…
-
4
votes1
answer1434
viewsProcedure with IF NOT EXISTS
Hello stackoverflow developers, I started to delve into sql commands a little while ago, I have some stupid error of syntax of this project, but for countless attempts, I could not Scan it, follow…
-
4
votes1
answer284
viewsIs it possible to create a decision structure in a table in SQL?
I have a massive database, has more than 2,000 lines, and the second column has an ENUM that allows only 'Option 1' and 'Option 2'. If I insert 200 lines into the table and write the wrong ENUM name…
-
4
votes1
answer1037
viewsConsult ID in database ignoring scores
I have a mysql database where I need to make a comparison of registered Rgs with another provided by the user through an input, to see if the same ID is already registered in the database. The…
-
4
votes4
answers2981
viewsSQL Query Joining 5 tables
I need help to do a query (multiple relationships) on my Webservice + Mysql to return the result on my Android app. I have these respective tables: Query I need to query a person(tb_suspect), with…
-
4
votes1
answer743
viewsOUTER APPLY does not work like INNER JOIN
I know that the OUTER APPLY command is analogous to INNER JOIN, but I am confused about a situation where this does not happen. The query below extracted from the Training Kit 70-461: SELECT…
-
4
votes4
answers249
viewsMost common words among Rows
Is there any function in Mysql that gives me the 3 most common words between a column in format TEXT one-table? Example Maria Joaquina Maria Antonienta Maria B. among these Rows he returns Mary…
-
4
votes3
answers527
viewsStructure/Nomenclature of Tables
I am developing a permission system and I have a universe of 9 tables, which form the final permission for the user to use my systems, however, I am in doubt of how to build two of them, the…
-
4
votes1
answer410
viewsPhonetic Research for Telephone Directory
I’m having a problem searching for phrases, I have a search field and I’m using the LIKE operator to do this, but it’s not serving me correctly, I tried now using SOUNDEX, but it only returns a word…
-
4
votes2
answers7173
viewsFirebird SQL auto-increment
I’m taking an SQL/Modeling course in mysql the creation of a composite table would look like this: USE banco; CREATE TABLE clientes( cli_id integer not null auto_increment, cli_nome varchar(20) not…
-
4
votes2
answers872
viewsWhat is the best way to create a conditional foreign key Constraint?
In some cases, it is interesting to create a foreign key reference conditional, that is, that does not allow to reference all the records of another table, but only those that assume a condition.…
-
4
votes3
answers30960
viewsHow to query in Mysql with two different conditions in WHERE
I have two queries that work perfectly separately, but I needed all their records together. Would be these: SELECT b.idBanca AS idB, b.DataHora AS dataHora, b.Sala AS sala, t.idTrabalho AS idT,…
-
4
votes1
answer3803
views -
4
votes3
answers1446
viewsSpecify fields I don’t want in Mysql
In Mysql, when we will make a SELECT, generally we can specify each field we want to return. SELECT id, nome FROM usuarios But let’s imagine a scenario where I have 50 columns in a table and I…
-
4
votes3
answers12716
viewsSQL Grouping by name, date and quantity by month
I am not able to group this result per month ("PP.PAPELETA_DATA" field). Type the quantity (Count(PD.Personal) as Total) separated by month! Could someone give me a hand? Usage SQL SERVER 2012.…
-
4
votes2
answers201
viewsHow to save space in audit log?
I was creating a system that provides me data of my users according to the date I request, from X to Y for example. And the only way I could find for that was to create a table that way: date with…
-
4
votes3
answers2878
viewsWhat is the best way to verify the existence of a record in the database via application?
Today I use a method that sends a COUNT with the record id as a parameter for the database. There is a better, perhaps more performative way to do this?
-
4
votes1
answer1246
viewsIn Postgresql, which field to use for percentage?
In the table that I am creating, I will have a column that will indicate the discount percentage, for example, "for a given agreement I will have 20% discount, for another agreement I will have 50%…
-
4
votes2
answers370
viewsHow to select from a night period (18hrs to 6hrs)
Description: Count how many times the action occurred in the night period considering the time from 18:00 to 06:00 in the morning. There is a better way to select this type without having to use or…
-
4
votes2
answers98
viewsDoubt while sql
I need to get all five figures before the ~3L of the XXX: ~1BLB~2B1C~3L~1TPL~2B39~3L~1RAD~2C1D~3L And so on and so forth. I already managed to make it takes the first value before the 3L with this…
-
4
votes2
answers29746
viewsHow to pass data from one table to another SQL
I am doubtful to pass data from one table to another: Example: Table 1 --> Table 2 ---> delete all information from Table 1 I am creating the project in VB.NET and using SQL…
-
4
votes1
answer2185
viewsInsert cities into database
Hello, I am creating a database, and in it I will have information of Brazilian municipalities, I imagine that I am certainly not the first to need this information, however I do not know if there…
-
4
votes1
answer75
viewsWhen should I create a Constraint like DEFERRED?
As far as I know, creating a Constraint (like a foreign key or a check) like DEFERRED makes it only validated at the end of the transaction. What is this used for? And when it should not be used?…
-
4
votes3
answers619
viewsDELETE FROM users WHERE user_id = $id
I have a script simple PHP+Mysql query: <table > <tr> <td ><h2>GERENCIAR ANUNCIOS </h2></td> </tr> <tr> <td > <table > <tr> <th…
-
4
votes1
answer164
viewsInvert 0 or 1 in a column
I have a table that has numerous records and a column called posição. It only gets the value 0 for locked and 1 for released. There now with a change I need to reverse these values, where this 0 has…
-
4
votes3
answers1500
viewsCopy bank record
I used the following query to try to 'copy' a record insert into valores select * from valores where id = 1 However, I receive the following message: An Explicit value for the Identity column in…
-
4
votes1
answer1076
viewsField SUM of two MSSQL Sub-consultations
I need a field derived from two sub-consultations that gives the sum of values based on a field. Follow example: SQL: SELECT CodCliente ,DataVenda ,Vendedor ,Valor ,DataPagamento ,TotalCliente…
-
4
votes4
answers10030
viewsMysql error 1054 Unknown column
I’m making an SQL that counts how many names in the table start with any letter but an error is being returned: Column not found: 1054 Unknown column 'A' in 'Where clause' To the following SQL:…
-
4
votes2
answers304
viewsHow to store the Selecteditem value from Dropdownlist in the database?
I want to register a client and one of his attributes is sex Database: the sex attribute is a nvarchar ASP.NET: I am working with a listview to enter client data <asp:SqlDataSource ID="clientes"…
-
4
votes1
answer5879
viewsRecursive query
I need to mount a recursive query to solve a simple problem, but I’m having a little trouble. I have a table called TABELA1 with the following fields (ID, IDPAI, NOME) I will put here some examples…
-
4
votes2
answers21222
viewsSimple record editing with PHP+Mysql
Basically the three scripts should edit a record in the Database: php records.: <?php // Aqui você se conecta ao banco $mysqli = new mysqli('127.0.0.1', 'root', '', 'login'); // Executa uma…
-
4
votes1
answer6466
viewsAdd Mysql Time Field
Dear friends, I have a table with a TIME column, which stores the time worked on the day, and I am trying to sum up these times at the end of a month, but without success. I used the following…
-
4
votes2
answers2774
viewsMysql - make each row increment 1
I want to make an appointment sql and I want each row to have a column indicating the number of the ex row: linha nome 1 joao 2 maria 3 tiago . . . . . . n joares But this "row" column I don’t have…
-
4
votes1
answer795
viewsUse Execute block in SQL
Is there any way to search two fields (in different tables), compare them and if they are different perform an update? Could you give me a brief example if there is any way, using the EXECUTE BLOCK.…
-
4
votes1
answer240
views -
4
votes2
answers3709
viewsHow do I update multiple rows of a table after it undergoes a certain change?
CREATE or REPLACE TRIGGER TG_ATUALIZA_BLOQUEIO AFTER UPDATE ON pcclient FOR EACH ROW begin if :NEW.BLOQUEIO = 'S' THEN UPDATE PCCLIENT SET BLOQUEIO='S' where codcliprinc=:OLD.CODCLIPRINC; END IF;…
-
4
votes1
answer415
viewsHow to send data from one table to another with the Deleted command
Guys I’m creating this trigger in the SQL Server, but I’m not getting the id to send the data to another table and delete from this table. CREATE TRIGGER MoveComprador ON comprador INSTEAD OF DELETE…
-
4
votes2
answers174
viewsThe less consultation the better, right?
It is possible to vary the writing of the results, as an example below where I want to use. <table width="1000" align="center"> <tr> <td> NOTICIAS</td>…
-
4
votes4
answers3989
viewsHow to display 2 columns from 2 different tables in Mysql?
I have 2 tables with multiple columns. I would like you to display individual columns of more than one table at the same time: tabela1 | tabela2 Nome | Apelido…
-
4
votes1
answer319
viewsApostrophe within echo
well I’m trying to put on the slide the images but the bet is breaking , where is the error? edited below the summary after the tests I did even using the \ . meu codigo atual echo "<div…
-
4
votes3
answers5303
viewsFirebird, Insert com Where not exists
I am trying to insert a record into the table, but this record will only be inserted if it no longer exists there, so I am trying to do so: INSERT INTO R01 (NUMERO,EXPORTADOR, IMPORTADOR,…
-
4
votes3
answers685
viewsDoubt with Getdate()?
I have an SQL query that should bring me only records whose date is longer than the date of the day, but records with the current date are coming. Thanks! --não deveria mostrar a data de hoje select…
-
4
votes3
answers4174
viewsRecover larger and shorter hours conditioned on dates
Inside a table containing the columns data_inicio, data_fim, hora_inicio, hora_fim need to identify within a set of records the following occurrences: the lowest start date, the highest end date,…
-
4
votes1
answer343
viewsHow to get the value before MAX in SQL?
How can I get the value before the MAX in SQL? I tried to MAX -1 but it did not work, because the values of the column I want to select the value before the MAX are not in ascending order.…
sqlasked 9 years, 3 months ago Amanda Lima 3,428 -
4
votes1
answer181
viewsHow to avoid duplication of content in a table without a primary key?
In a virtual shop panel I am building the products have relations by color and size, where each product color has a size. This is a part of my database to better understand the problem:…
-
4
votes2
answers320
viewsSQL Server - Doubt update and relation between tables
I have two tables, one call coletor, and another call material, have a proc calling for cadMat, that was to register material in the bank and by Cpf (parameter of proc) put as the name of the…
-
4
votes3
answers1334
viewsSQL query in PHP and date format
I have a PHP function that interacts with my SQL Server. It looks like this: public function buscaRelatorioHospedagem($nome,$de,$ate){ // TODO Essa função pode ser aproveitada pra aba nova $de--;…
-
4
votes2
answers1552
viewsSQL query with array type in Postgresql
How to filter this field of type array in Postgresql? I have a table X with a field Y of the kind character varying[] with two records: {'meeting','lunch','training','presentation'}…
-
4
votes2
answers3761
viewsHow to select all id’s except one?
Is there any way in SQL I get all the data except an ID? For example (I know this does not exist in SQL) Select * From Utilizadores Where id_ut != 1;