mysql> describe skin; +--------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------------+------+-----+---------+----------------+ | skinid | int(10) unsigned | NO | PRI | NULL | auto_increment | | skin_name | varchar(15) | NO | | NULL | | | skin_way | varchar(32) | NO | | NULL | | | date_skin | int(10) unsigned | NO | MUL | NULL | | | skin_count | int(10) unsigned | NO | | NULL | | | uid_skinfrom | int(10) unsigned | NO | MUL | NULL | | | skcatid_skin | int(10) unsigned | NO | | NULL | | +--------------+------------------+------+-----+---------+----------------+ 

It is necessary to drag out the values ​​from this TB, in one line, but in TWO columns:

Need to get the value of skin_count but for different skinid. In the example I will give two requests that need to obezhenit. I know that you can use the reassignment of the parameters mysqli_stmt_bind_param (), but is it possible to pull out two values ​​in one line?

Here are examples of requests (they need to be combined into one, well, uh, if this is possible?):

 select skin_count from skin where skinid=$_SESSEION['cur_skin']; select skin_count from skin where skinid=$new_skinid; 

I want to get something like

 +--------------+-----------+ | oldcount | newcount | +--------------+-----------+ | 48 | 25 | +--------------+-----------+ 

How to do this as ʻami or nested questions or something else?

    3 answers 3

     SELECT MAX(CASE skinid WHEN $_SESSION['cur_skin'] THEN skin_count END) oldcount , MAX(CASE skinid WHEN $new_skinid THEN skin_count END) newcount FROM skin; 

      select (select skin_count from skin where skinid={$_SESSEION['cur_skin']}) as curskin, (select skin_count from skin where skinid=$new_skinid) as newskin

      • With subqueries it is more clear to me. I will understand ... - root_x Povierennyy
      • see Akin's answer - learn the right one) - Majestio
      • I agree, my version is the most clumsy))) - AlexandrX
      • This option for me yesterday was the most understandable, but now I will use the other. I want to ask, "what's the ALWAYS?" A lot of subqueries? I mean, I asked a question (hmm-m ... how interesting it sounds) about using ONE instead of TWO requests, but in your decision you can get THREE requests? In this toprnost? - root_x Povierennyy
      • one
        yes, that is why ... well, more precisely, a query to the sql server is one (and this is better than two explicit queries), but under the hood, there will most likely be two calls to the table (the optimizer can sometimes be cunning, but in this case, I doubt that it will cope), so the option is not optimal (the third subquery doesn’t refer to the tables, so it almost does not count). The actual difference in execution time, of course, depends on the amount of data, and is likely to be negligible, but all the same, other things being equal, it is better to do it immediately with Feng Shui :) - AlexandrX

      You can still like this:

       select A.skin_count, B.skin_count from skin A, skin B where A.skinid=$_SESSEION['cur_skin'] and B.skinid=$new_skinid; 

      But with restrictions - both IDs are guaranteed to be in the database. If at least one ID does not appear - the request will not return anything.

      • because the condition is AND. This is how I am :). - root_x Povierennyy
      • In general, I am a little afraid of strange points and as'y in your answers. Frightening because I do not pony ... I know little and understand in sql. But somehow I chuyut delve into all this. - root_x Povierennyy
      • @root_xPovierennyy And what's so strange. we take the skin table twice, so that we can accurately understand from which table that we will call one A, another B. When referring to the fields, using a point, we specify from which table to take the field - Mike
      • I want to buy the book Graber (Gruber) Introduction to sql. Should I buy it? I read a lot of reviews - everyone writes that everything is perfectly explained there. Maybe there is some better book? And here's what else !: I have it, BUT in electronic format - BUT I really don’t like to read a lot of text from the monitor (I ruined my eyes a little bit like that a couple of years ago.) Buy me a paper book? - root_x Povierennyy
      • @root_xPovierennyy I do not know what kind of SQL literature there is. I never read anything. In addition to the manuals for specific DBMS. and you can also look at the answers here . Stackoverflow.com/… ... - Mike