My SQL table called categories has the following structure: I would like to know if it’s possible to JOIN the same table and present the parent_id name. I can do this by PHP code, but I would like, because of performance issues, to retrieve as SQL Query. See example at SQLFiddle I’ve managed to JOIN the table, but somehow the
Tag: mysql
MYSQL toggle enum values on each call
I want this to be like this. I have an ENUM column with 0 AND 1, i want to toggle between these two values on each query. I tried this, but it leaves the column empty. Thanks. Answer I don’t like enum for various reasons. Especially when you use it to store 0 and 1 it can be confusing, yes,
How to give comments in SQL
I am working in SQL. What is the complete explanation for using comments in SQL, with some example code? Answer MySQL Server supports three comment styles: From a # character to the end of the line. From a — sequence to the end of the line. In MySQL, the “– ” (double-dash) comment style requires the second dash to be
MySQL/MariaDB – order by inside subquery
I used the following query with MySQL 5.5 (or previous versions) for years without any problems: SELECT t2.Code from (select Country.Code from Country order by Country.Code desc ) AS t2; The order …
Huge performance difference between two similar SQL queries
I have two SQL queries that provides the same output. My first intuition was to use this: Now, this took something like 70 secs to complete! Searching for another solution I tried something similar: Surprisingly, this took 0.05 secs to complete!!! how come these two are so different? thanks! Answer First thing which Might Cause the Time Lag is that
PHP fails to insert into Mysql (auto increment)
I have made my Mysql Table link this: That problem occurs because i am not able to insert auto_increment into my mysql query. index.php I post to it by a HTML form and it does executes the query but i get no rows in that table. I thnk its because of auto_increment or Unique or Primary. i was told to
How to fetch multiple columns from multiple table and show data on non empty columns and null or zero on empty columns using joins in MYSQL
I would like to know how to fetch multiple columns from multiple table even if one column in one table does not exist and return the columns which ever exist in one or more table used for joins Ex: Tables: SQL: Problem: What I need? If a column doesn’t exist in one table then return the rest of the columns
Random and duplicates with order by?
Suppose I have a table like so, What I need to do, is randomize this SELECT query, but in a very two particular ways that I just can’t figure out how to do. Firstly, I want unique_data randomized, so that the SELECT query could return something like (randomly): The second requirement I have is that, unique_data appears multiple times, but
Laravel: getting a single value from a MySQL query
I’m trying get a single value from MySQL database using laravel but the problem I’m getting an array . this is my query result in MySQL command line: my laravel function: expected $data value is a string with value= Admin but what i get is : [{“groupName”:”Admin”}] Answer Edit: Sorry i forgot about pluck() as many have commented : Easiest
Get minimum unused value in MySQL column
I have a table with integer ID column. I would like to get the minimum unused value for this column. The query should find the first hole in table IDs and get the minimum value inside it. I’ll try to explain it with some examples. Example 1: no-holes table In this case, I have a table without holes and query