Most voted "select-sql" questions
SELECT is an SQL statement that returns zero or more rows, from one or more base tables, temporary tables, or views in a database.
Learn more…241 questions
Sort by count of
-
60
votes8
answers16426
viewsWhy is using "SELECT * FROM table" bad?
It is often said to take all columns of a table through the command SELECT * FROM tabela is a bad practice. Any recommendation without explanation is not helpful. So... If it is bad practice, there…
-
36
votes8
answers7830
viewsIs giving a "SELECT" before an "INSERT" a safe way of not having duplicate records?
I have a table with a column codigo. I cannot set as primary key. In a system it receives many notifications before it checks with a SELECT to see if that code already exists in the table. If it…
-
32
votes2
answers6682
viewsWhat would be the way to validate the CPF checker digits in a DB using only a SELECT?
Finding information on how to calculate the CPF check digits is not difficult. Any Google search gives numerous results. Most are reliable and even have a well respected source which is the entry on…
-
24
votes4
answers4178
viewsWhat is the difference between joining tables by JOIN and WHERE?
What is the difference between joining tables by JOIN or by WHERE? Examples: SELECT * FROM clientes c JOIN enderecos e ON c.id = e.id_cliente; SELECT * FROM clientes c, enderecos e WHERE c.id =…
-
19
votes3
answers9682
viewsWhat’s the difference between Where and having?
Doing some tests on a database that in Mysql, I realized that: SELECT * From client WHERE uuid = '1kvsg4oracxq' returns the same result as: SELECT * From client HAVING uuid = '1kvsg4oracxq' What…
-
10
votes2
answers3515
viewsWhat is the difference of an empty string and NULL in SQL?
What’s the difference of storing a string as NULL or empty in SQL? How these two can behave when I go to make one SELECT, or INSERT worthwhile '' in that column which is of the type varchar? If I…
-
9
votes2
answers5943
viewsConvert row to column
I have a field called "Shift" kind STRING, storing data in Mysql in this way: '1,2,3,4,5,6,7,8,9' I need this string line to transform (transpose) into a single column (in this case, it would be the…
-
9
votes3
answers49197
viewsHow to use the output of a select to mount another?
I built a select that concatenates a string. I want to use the result of this select in another select SELECT * FROM (SELECT CONCAT('caminho da tabela') AS caminho FROM tabela) AS result Can you do…
-
8
votes7
answers54187
viewsRemove letters and special characters in a select
I have in my table the field documento with the following data: How do I select only the numbers of these records by removing the letters and special characters? DOCUMENT CPF-12345698-35…
-
8
votes3
answers650
viewsWhat is the best way to build a Mysql table with recipe items? All ingredients in one row or one ingredient per row?
I’m used to creating websites with few database records, so I don’t care about the speed of query and I have no experience with large volumes of information. But now I am creating a website recipes,…
-
7
votes1
answer489
viewsCompare COUNT from two tables
I have two tables and I must return the total of the table with more data, I am doing so: SELECT CASE WHEN ((select count(*) as `familiar` from tb.familiar)) > ((select count(*) as `personal`…
-
6
votes1
answer876
viewsCombination of 4 numbers in Mysql
SGBD: Mysql Problem: List all possible combinations between the numbers "1,2,3,4", so that they do not recur between them. Example: 1 2 3 4 12 13 14 21 23 24 31 32 34 41 42 43...…
-
6
votes1
answer5097
viewsHow to group by month with SQL?
I have a table, for example, with an attribute nome and data (for example only). I would like to generate a query that returns the amount of each row grouped by name and month: Nome Janeiro Fev…
-
6
votes3
answers25254
viewsSubtract date and display in YEARS, MONTHS and DAYS
SGBD: Mysql Problem: How to replace the current date with a specific date and return the value in YEARS, MONTHS and DAYS in a query? Example: DADOS DataAdmissao dataAtual 2010-04-07 2014-06-27 (este…
-
6
votes1
answer187
viewsI cannot display the SQL value
I am trying to start a connection to the Mysql database and it works, but when I ask PHP to display what is there nothing appears. It’s like the connection to the BD had failed. <?php //…
-
6
votes3
answers401
viewsHow to create Mysql search that returns lines without matching?
I would like to know how I can do a search that returns records that are not part of the research condition. For example, think about the situation where there is a database of a movie rental…
-
6
votes1
answer1251
viewsHow to filter separate dates by day , month and year in Mysql
Hello, I have a table "Meetings" in Mysql with the following structure: id:Int(11), subject:String(40), day:String(2), mes:String(2), year:String(4), active:String(2) except in the field day the day…
-
6
votes2
answers1019
viewsselect bringing all un-duplicated records based on a single column
How can I bring all records from a column that are not repeated. For example: | tela | url | perfil | dataCriacao | --------------------------------------------- | dica | /dica/ | ROLE_CDL |…
-
5
votes2
answers18687
viewsHow to add values of a field grouped by date?
SELECT funcionario_id, data, avaliacao_postura FROM equipe id funcionario_id data avaliacao_postura 1 1 2014-03-02 -25;-10;-5;-12 2 1 2014-03-01 -25;-10;-18 3 1 2014-03-02 -25;-15;-14 I need to make…
-
5
votes3
answers51716
viewsSQL Server - Check duplicate data in two simultaneous fields
DBMS: SQL Server 2014; Problem: I need to know which records are duplicated, so that it is checked in two simultaneous fields, that is, if the field "CPF" with the value '83971465842' and the field…
-
5
votes1
answer5102
viewsRelate two mysql tables to data from the same column?
I am creating a 2014 World Cup table. I created two tables. times (ID, nome, sigla, bandeira) jogos (ID, fase, local, time1_id, time2_id, data) I have a relatively simple problem, but I found no…
-
5
votes2
answers5263
viewsHow to calculate the difference between values that are in the same column?
I would like to know if there is any way to calculate the difference between values that are in the same column. For example: I have a table with 2 fields: date and balance. I need to add a new…
-
5
votes1
answer92
viewsError with Like do sql
How do I extract all fields starting with '1. ' from the BD? In my instruction I used the LIKE but had no return, but to remove the point already returns something, but returns fields I do not want,…
-
5
votes1
answer186
viewsPerform random SELECT
I have a table where I always want to show a single record randomly. I’m doing like this: SELECT `id` FROM `tabela` ORDER BY RAND() LIMIT 1 I see that in this way the same records repeat a lot,…
-
5
votes3
answers22753
viewsError in select using mysqli_query
I made a php to list the rooms, but I’m having difficulty to list them, if anyone can help me, I would appreciate <?php $link = mysqli_connect("localhost","root","","hotel"); $result =…
-
5
votes2
answers69
viewsSort a Select by criteria outside the database
I would like to create a SELECT with the personal relationship here of my company. SELECT função, nome FROM responsavel; So far so good. But as ordered by the Manager, passing by the Incumbent,…
-
4
votes3
answers3146
viewsError when performing a database query
I’m using Zend and have the following function: public function getChamado($id) { try { $cols = array( 'id', 'titulo', 'descricao', 'fk_status', 'fk_local', 'fk_tipo', 'created', 'modified',…
-
4
votes1
answer391
viewsWhat is the weight of a subquery for the query?
During the development of some queries in the database, I come across the situation of the need to perform a new query, but another solution in some cases may be the subquery. So what is the best…
-
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
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
votes1
answer4598
viewsHow to list a sample of all tables in a Mysql database?
I need to sample all tables in a Mysql database, for example through a SELECT * ... LIMIT 10. I already have the code that returns all tables of the current database: select table_name from…
-
4
votes1
answer1056
viewsTracking and posting system (social network type)
I’m developing a new project and I’m creating a profile following system where the goal is to show only the publications of those I follow. Such as a social network. I follow the person, so I get…
-
4
votes1
answer297
viewsSelect by group
Good morning guys, I would like to know how to do the following SELECT in mysql: Having the following table, I need to select the last 3 purchases of each Name: From now on, thank you…
-
4
votes3
answers158
viewsDoubt about sql query
I have a question. I believe it is nothing complex, but I could not solve. For example, I have this query: select * from Paciente where ClinicaID = 3 Your result would be these 3 records:…
-
4
votes2
answers948
viewsHow to get random results in SQL with different Dbms?
Based on this existing question on Soen and wanting to bring interesting and useful content to Sopt I ask this question: How to get random results in SQL with different Dbms? I’m leaving an answer…
-
4
votes1
answer68
viewsDifference between query with and without Inner Join
What is the difference between doing the two queries below, one with and the other without INNER JOIN? What is the most appropriate? SELECT p.Descricao as Produto, c.Descricao as Categoria FROM…
-
4
votes1
answer416
viewsHow to group Counts for different queries into one?
I need to make an SQL query to count indications, I have the following querys: SELECT 'FACEBOOK', COUNT(id) FROM `clientes` where indicacao like '%face%' SELECT 'Instagram', COUNT(id) FROM…
-
4
votes2
answers97
viewsSearch for lower values of one field according to values of another
I have this data entered in my table. mysql> SELECT * FROM db_cotacao; +----+------------+-------+------------+--------+---------+ | id | data | valor | validade | idforn | idativo |…
-
3
votes1
answer247
viewsCheck records between today and 10 days in the future to send by email
I’m using Phpmailer to send an email with data when it’s 10 days away. When I send the email, all the records are showing up when the idea is to show up the logs between today and 10 days in the…
-
3
votes1
answer475
views"Linked Server" with Mysql
DBMS: SQL SERVER 2014 + Mysql 5.5 What are the syntax of SELECT, INSERT and UPDATE instead of using the "OPENQUERY" function, when connecting through the SQL SERVER Linked Server, connecting in…
-
3
votes2
answers6065
viewsHow to add 3 columns of different tables
I am developing a financial system. Today we have the following situation: Banks (id, name, outstanding); Recipes (id, geraParcela, qtyParcelas) Receitas_installments (id, id_recipe, value,…
-
3
votes3
answers9315
viewsSum results values in mysql
I am servicing a bank and am finding it difficult to make a select. Table structure descontos_taxas: id | value | client | data_created My Scenario: This table holds both rates and discounts in the…
-
3
votes1
answer4451
viewsDo not repeat data when making a SELECT in Mysql
What I want is this. I’m working on a system, which registers an employee and days that he worked on during the month. So far so good, but it happens that in some cases, the employee gets 2 or more…
-
3
votes1
answer476
viewsConvert column to row
This is the select of my table, the query I am using follows in the image below: It brings the normal result, but wanted it to bring this way: | Id_unica | Pixel | Inch | Quantity | 18 1080p 15 2 Is…
-
3
votes3
answers1773
viewsHow to bypass an accent in mysql + Php
I have in my database words with accents and when I want to call I use this SQL below. But if a word in the database has accent I need to also use the word accented in my search, otherwise nothing…
-
3
votes1
answer4108
viewsDoubts about column formatting in gridView
Hi, I have some questions about formatting the columns in a gridview, I have a gridView that is populated by data coming from a table in the database, the columns and row are generated…
-
3
votes1
answer157
viewsDatabase query refactoring with multiples FIND_IN_SET()
I have the following query to find related topics by a set of ID’s that are not the topic to be viewed: SELECT press.image, press_i18n.title, press_i18n.slug FROM press INNER JOIN press_i18n ON (…
-
3
votes1
answer539
viewsChanging a field’s value in a select
Query: select id_cliente,nm_cliente,tp_sexo from tb_clientes; In my bank sex is registered as int I wish that when I make a select display Male or Female in place of 1 or 2, how do I do this?…
-
3
votes1
answer92
viewsSQL Server Rotary Records
I would like to know how to select records rotationally from a table. For example I have a table called disclosure and I want the records to be selected in such a way that today brings the element…
-
3
votes1
answer4727
viewsSelect the 3 highest values
I have a table that has the following data: id_indication, id_user,lg_client A user can indicate multiple clients (lg_client = 1), would like a select that searches for the 3 users that indicated…